pentaho/pentaho-kettle · error · RuntimeException

Could not convert the String with the given format :" +…

Error message

Could not convert the String with the given format :" + e.getMessage()

What it means

In str2num's two-argument case str2num(s, pattern), the string is parsed with a DecimalFormat constructed from the user-supplied pattern; any exception (ParseException when the string does not match the pattern, IllegalArgumentException for a bad pattern) is wrapped in this RuntimeException.

Solutions

  1. Ensure the input string exactly matches the DecimalFormat pattern, including separators.
  2. Validate the pattern is legal Java DecimalFormat syntax.
  3. Use the 3-argument str2num(s, pattern, locale) when data comes from a specific locale.
  4. Pre-clean the string (remove symbols, normalize separators) before parsing.
  5. Catch the RuntimeException and log/redirect bad rows to an error stream.

Example fix

// before
var n = str2num("1,234.56", "#.##"); // string does not match pattern
// after
var n = str2num("1,234.56", "#,##0.00"); // pattern matches input
Defensive patterns

Strategy: validation

Validate before calling

function parseFmtSafe(s, p) {
  if (typeof s !== "string" || typeof p !== "string") return null;
  if (!/^[#,0.0;%\-+ ]+$/.test(p)) return null; // basic pattern sanity
  return s;
}

Try / catch

try { return str2num(s, pattern); } catch (e) { return Number.NaN; }

Prevention

When it happens

Trigger: Calling str2num(s, pattern) where s does not conform to pattern — e.g. str2num("12,34", "#,##0.00") — or where the pattern itself is malformed for DecimalFormat.

Common situations: Pattern/data locale mismatch (grouping vs decimal separator); data rows violating the expected fixed format; patterns copied from Excel or another formatting library; misaligned thousands separators.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of pentaho/pentaho-kettle@f3058517a1 (2026-09-13). Data as JSON: /api/errors/d7f97d3c23f9289a. Report an issue: GitHub.

Appendix: source

Thrown at engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java:1405

        }
        break;
      case 2:
        try {
          if ( isNull( ArgList, new int[] { 0, 1 } ) ) {
            return new Double( Double.NaN );
          } else if ( isUndefined( ArgList, new int[] { 0, 1 } ) ) {
            return undefinedValue;
          }
          String sArg1 = (String) ArgList[0];
          String sArg2 = (String) ArgList[1];
          if ( sArg1.equals( "null" ) || sArg2.equals( "null" ) ) {
            return null;
          }
          DecimalFormat formatter = new DecimalFormat( sArg2 );
          dRC = ( formatter.parse( sArg1 ) ).doubleValue();
          return new Double( dRC );
        } catch ( Exception e ) {
          throw new RuntimeException( "Could not convert the String with the given format :" + e.getMessage() );
        }
        // break;
      case 3:
        try {
          if ( isNull( ArgList, new int[] { 0, 1, 2 } ) ) {
            return new Double( Double.NaN );
          } else if ( isUndefined( ArgList, new int[] { 0, 1, 2 } ) ) {
            return undefinedValue;
          }
          String sArg1 = (String) ArgList[0];
          String sArg2 = (String) ArgList[1];
          String sArg3 = (String) ArgList[2];
          if ( sArg3.length() == 2 ) {
            DecimalFormatSymbols dfs = new DecimalFormatSymbols( EnvUtil.createLocale( sArg3.toLowerCase() ) );
            DecimalFormat formatter = new DecimalFormat( sArg2, dfs );
            dRC = ( formatter.parse( sArg1 ) ).doubleValue();
            return new Double( dRC );
          }

View on GitHub (pinned to f3058517a1)