{"record":{"id":"555c308485e5cc6a","repo":"pentaho/pentaho-kettle","slug":"could-not-convert-the-string-with-the-given-format","errorCode":null,"errorMessage":"Could not convert the String with the given format :","messagePattern":"Could not convert the String with the given format :","errorType":"exception","errorClass":"RhinoRuntimeError","httpStatus":null,"severity":"error","filePath":"engine/src/main/java/org/pentaho/di/trans/steps/scriptvalues_mod/ScriptValuesAddedFunctions.java","lineNumber":1439,"sourceCode":"        }\n        break;\n      case 2:\n        try {\n          if ( isNull( ArgList, new int[] { 0, 1 } ) ) {\n            return new Double( Double.NaN );\n          } else if ( isUndefined( ArgList, new int[] { 0, 1 } ) ) {\n            return Context.getUndefinedValue();\n          }\n          String sArg1 = Context.toString( ArgList[0] );\n          String sArg2 = Context.toString( ArgList[1] );\n          if ( sArg1.equals( \"null\" ) || sArg2.equals( \"null\" ) ) {\n            return null;\n          }\n          DecimalFormat formatter = new DecimalFormat( sArg2 );\n          dRC = ( formatter.parse( sArg1 ) ).doubleValue();\n          return new Double( dRC );\n        } catch ( Exception e ) {\n          throw Context.reportRuntimeError( \"Could not convert the String with the given format :\"\n            + e.getMessage() );\n        }\n        // break;\n      case 3:\n        try {\n          if ( isNull( ArgList, new int[] { 0, 1, 2 } ) ) {\n            return new Double( Double.NaN );\n          } else if ( isUndefined( ArgList, new int[] { 0, 1, 2 } ) ) {\n            return Context.getUndefinedValue();\n          }\n          String sArg1 = Context.toString( ArgList[0] );\n          String sArg2 = Context.toString( ArgList[1] );\n          String sArg3 = Context.toString( ArgList[2] );\n          if ( sArg3.length() == 2 ) {\n            DecimalFormatSymbols dfs = new DecimalFormatSymbols( EnvUtil.createLocale( sArg3.toLowerCase() ) );\n            DecimalFormat formatter = new DecimalFormat( sArg2, dfs );\n            dRC = ( formatter.parse( sArg1 ) ).doubleValue();\n            return new Double( dRC );","sourceCodeStart":1421,"sourceCodeEnd":1457,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/engine/src/main/java/org/pentaho/di/trans/steps/scriptvalues_mod/ScriptValuesAddedFunctions.java#L1421-L1457","documentation":"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.","triggerScenarios":"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', '##,###').","commonSituations":"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).","solutions":["Verify the string matches the DecimalFormat pattern exactly, including thousands and decimal separators for the JVM's current locale","Pass a third locale argument, e.g. str2num(value, '#,##0.00', 'de'), to match the source data's locale","Pre-check the input with isNum(value) before calling str2num","Strip or normalize non-numeric characters (currency symbols, spaces) before conversion","Fall back to plain Number()/parseFloat if the input has no meaningful format"],"exampleFix":"// before\nvar n = str2num('1.234,56', '#,##0.00'); // throws: separators don't match pattern\n// after\nvar n = str2num('1.234,56', '#,##0.00', 'de'); // parse with German locale","handlingStrategy":"validation","validationCode":"function safeStr2num(v, fmt) {\n  if (v === null || v === undefined || v === '') return null;\n  if (!/^[0-9.,+\\- ]+$/.test(String(v))) return null;\n  return str2num(String(v).trim(), fmt);\n}","typeGuard":"function isNumericLike(v) {\n  return v !== null && v !== undefined &&\n    !isNaN(Number(String(v).replace(/[.,]/g, function(m){ return m === ',' ? '.' : ''; })));\n}","tryCatchPattern":"try {\n  var n = str2num(value, '#,##0.00');\n} catch (e) {\n  if (e.message.indexOf('Could not convert the String with the given format') === 0) {\n    n = null; // log row key and fail the step deliberately if needed\n  } else { throw e; }\n}","preventionTips":["Always pass the third locale argument when data may come from mixed regions","Trim/clean currency symbols and whitespace before parsing","Test the transformation with edge rows (empty, 'N/A', locale-swapped numbers)","Use isNum() to gate str2num calls","Pin the JVM locale in kettle.properties so parsing is deterministic"],"tags":["javascript","decimalformat","number-parsing","pentaho","locale"],"backgroundTag":"invalid-argument-format","analyzedSha":"f3058517a153da500bf4551f46d79b91bf8ec552","analyzedAt":"2026-09-13T14:04:16.340Z","contentChangedAt":"2026-09-13T14:04:16.340Z","schemaVersion":2},"datasetVersion":"2026-09-20T23:17:15.980Z"}