{"record":{"id":"d7f97d3c23f9289a","repo":"pentaho/pentaho-kettle","slug":"could-not-convert-the-string-with-the-given-format-e","errorCode":null,"errorMessage":"Could not convert the String with the given format :\" + e.getMessage()","messagePattern":"Could not convert the String with the given format :\" \\+ e\\.getMessage\\(\\)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java","lineNumber":1405,"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 undefinedValue;\n          }\n          String sArg1 = (String) ArgList[0];\n          String sArg2 = (String) 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 new RuntimeException( \"Could not convert the String with the given format :\" + 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 undefinedValue;\n          }\n          String sArg1 = (String) ArgList[0];\n          String sArg2 = (String) ArgList[1];\n          String sArg3 = (String) 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 );\n          }","sourceCodeStart":1387,"sourceCodeEnd":1423,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java#L1387-L1423","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the input string exactly matches the DecimalFormat pattern, including separators.","Validate the pattern is legal Java DecimalFormat syntax.","Use the 3-argument str2num(s, pattern, locale) when data comes from a specific locale.","Pre-clean the string (remove symbols, normalize separators) before parsing.","Catch the RuntimeException and log/redirect bad rows to an error stream."],"exampleFix":"// before\nvar n = str2num(\"1,234.56\", \"#.##\"); // string does not match pattern\n// after\nvar n = str2num(\"1,234.56\", \"#,##0.00\"); // pattern matches input","handlingStrategy":"validation","validationCode":"function parseFmtSafe(s, p) {\n  if (typeof s !== \"string\" || typeof p !== \"string\") return null;\n  if (!/^[#,0.0;%\\-+ ]+$/.test(p)) return null; // basic pattern sanity\n  return s;\n}","typeGuard":null,"tryCatchPattern":"try { return str2num(s, pattern); } catch (e) { return Number.NaN; }","preventionTips":["Make sure the string exactly matches the DecimalFormat pattern including separators","Use the 3-argument form to pin the locale when data is locale-specific","Validate the pattern syntax before deployment","Route unparseable rows to an error stream instead of failing the transform"],"tags":["str2num","decimalformat","pattern","parse-error"],"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"}