pentaho/pentaho-kettle · error · RhinoRuntimeError

Could not convert the String with the given format :

Error message

Could not convert the String with the given format :

What it means

This error is thrown by the str2num JavaScript function (ScriptValuesAddedFunctions, a Rhino-visible helper in Pentaho Kettle/PDI). When str2num is called with 2 arguments (value string, format pattern), it builds a DecimalFormat from the second argument and parses the first; any failure in parsing — malformed number, wrong pattern, null-ish input — is caught and rethrown as a Rhino RuntimeException with this message. It means the string could not be converted to a number using the given format.

Solutions

  1. Verify the string matches the DecimalFormat pattern exactly, including thousands and decimal separators for the JVM's current locale
  2. Pass a third locale argument, e.g. str2num(value, '#,##0.00', 'de'), to match the source data's locale
  3. Pre-check the input with isNum(value) before calling str2num
  4. Strip or normalize non-numeric characters (currency symbols, spaces) before conversion
  5. Fall back to plain Number()/parseFloat if the input has no meaningful format

Example fix

// before
var n = str2num('1.234,56', '#,##0.00'); // throws: separators don't match pattern
// after
var n = str2num('1.234,56', '#,##0.00', 'de'); // parse with German locale
Defensive patterns

Strategy: validation

Validate before calling

function safeStr2num(v, fmt) {
  if (v === null || v === undefined || v === '') return null;
  if (!/^[0-9.,+\- ]+$/.test(String(v))) return null;
  return str2num(String(v).trim(), fmt);
}

Type guard

function isNumericLike(v) {
  return v !== null && v !== undefined &&
    !isNaN(Number(String(v).replace(/[.,]/g, function(m){ return m === ',' ? '.' : ''; })));
}

Try / catch

try {
  var n = str2num(value, '#,##0.00');
} catch (e) {
  if (e.message.indexOf('Could not convert the String with the given format') === 0) {
    n = null; // log row key and fail the step deliberately if needed
  } else { throw e; }
}

Prevention

When it happens

Trigger: Calling str2num('abc', '#,##0.00') or str2num('1,234.56', '#,##0.00') under a locale whose separators differ from the pattern; passing null/undefined as the value; using an invalid DecimalFormat pattern such as str2num('10', '##,###').

Common situations: Modified Java Script Value steps in Pentaho transformations converting formatted amounts from CSV/text files whose separators don't match the pattern; hardcoded patterns copied between locales (e.g. '1.234,56' European input parsed with a US pattern).

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/555c308485e5cc6a. Report an issue: GitHub.

Appendix: source

Thrown at engine/src/main/java/org/pentaho/di/trans/steps/scriptvalues_mod/ScriptValuesAddedFunctions.java:1439

        }
        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 Context.getUndefinedValue();
          }
          String sArg1 = Context.toString( ArgList[0] );
          String sArg2 = Context.toString( 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 Context.reportRuntimeError( "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 Context.getUndefinedValue();
          }
          String sArg1 = Context.toString( ArgList[0] );
          String sArg2 = Context.toString( ArgList[1] );
          String sArg3 = Context.toString( 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)