pentaho/pentaho-kettle · error · RuntimeException

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

Error message

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

What it means

In str2num's one-argument case, the string is parsed with a default DecimalFormat after Const.ltrim; any exception (ParseException for non-numeric text, NullPointerException on null, cast issues) is wrapped in this RuntimeException.

Solutions

  1. Verify the string matches the JVM default locale's number format (decimal separator, grouping).
  2. Strip non-numeric characters (currency symbols, spaces, trailing text) before parsing.
  3. Use the 2- or 3-argument str2num with an explicit pattern and locale matching the data.
  4. Trim the right side manually since only ltrim is applied.
  5. Catch the exception and return a fallback value (e.g. NaN or 0).

Example fix

// before
var n = str2num("1.234,56"); // fails in en_US locale
// after
var n = str2num("1.234,56", "#,##0.00", "de"); // parse with matching format/locale
Defensive patterns

Strategy: try-catch

Validate before calling

function parseSafe(s) { var t = (s == null) ? "" : String(s).replace(/[^0-9.,\-+]/g, ""); return t.length > 0 ? t : null; }

Type guard

function isNumericString(v) { return typeof v === "string" && /^\s*[+-]?[0-9.,]+\s*$/.test(v); }

Try / catch

try { return str2num(s); } catch (e) { // locale/format mismatch
  return Number.NaN; }

Prevention

When it happens

Trigger: Calling str2num(s) where s is not parseable as a number under the default locale — e.g. "1.234,56" text in an en_US locale, or strings containing currency symbols or letters.

Common situations: Locale mismatch between data source and JVM default (comma vs dot decimal separators); leftover units/currency symbols; whitespace at the right side (only the left is trimmed); CSV fields that failed earlier cleansing.

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/217e5c0640950084. Report an issue: GitHub.

Appendix: source

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

    double dRC = 0.00;
    switch ( ArgList.length ) {
      case 0:
        throw new RuntimeException( "The function call str2num requires at least 1 argument." );
      case 1:
        try {
          if ( isNull( ArgList[0] ) ) {
            return new Double( Double.NaN );
          } else if ( isUndefined( ArgList[0] ) ) {
            return undefinedValue;
          }
          if ( ArgList[0].equals( null ) ) {
            return null;
          }
          String sArg1 = (String) ArgList[0];
          DecimalFormat formatter = new DecimalFormat();
          dRC = ( formatter.parse( Const.ltrim( sArg1 ) ) ).doubleValue();
        } catch ( Exception e ) {
          throw new RuntimeException( "Could not convert the given String : " + e.getMessage() );
        }
        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 ) {

View on GitHub (pinned to f3058517a1)