{"record":{"id":"c2cdd34d76f5b201","repo":"pentaho/pentaho-kettle","slug":"could-not-convert-the-given-string","errorCode":null,"errorMessage":"Could not convert the given String : ","messagePattern":"Could not convert the given String : ","errorType":"exception","errorClass":"RhinoRuntimeError","httpStatus":null,"severity":"error","filePath":"engine/src/main/java/org/pentaho/di/trans/steps/scriptvalues_mod/ScriptValuesAddedFunctions.java","lineNumber":1420,"sourceCode":"    double dRC = 0.00;\n    switch ( ArgList.length ) {\n      case 0:\n        throw Context.reportRuntimeError( \"The function call str2num requires at least 1 argument.\" );\n      case 1:\n        try {\n          if ( isNull( ArgList[0] ) ) {\n            return new Double( Double.NaN );\n          } else if ( isUndefined( ArgList[0] ) ) {\n            return Context.getUndefinedValue();\n          }\n          if ( ArgList[0].equals( null ) ) {\n            return null;\n          }\n          String sArg1 = Context.toString( ArgList[0] );\n          DecimalFormat formatter = new DecimalFormat();\n          dRC = ( formatter.parse( Const.ltrim( sArg1 ) ) ).doubleValue();\n        } catch ( Exception e ) {\n          throw Context.reportRuntimeError( \"Could not convert the given String : \" + e.getMessage() );\n        }\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 ) {","sourceCodeStart":1402,"sourceCodeEnd":1438,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/engine/src/main/java/org/pentaho/di/trans/steps/scriptvalues_mod/ScriptValuesAddedFunctions.java#L1402-L1438","documentation":"str2num parses its string argument with a default DecimalFormat and calls parse(...).doubleValue(). If parsing fails (non-numeric text, unexpected characters), the caught Exception is rethrown as this error with the exception message appended. The string could not be converted to a number under the current default locale's DecimalFormat rules.","triggerScenarios":"str2num(s) where s contains non-numeric text, a thousands separator inconsistent with the default locale (e.g. \"1,234.56\" under a German locale), currency symbols, whitespace beyond leading spaces (Const.ltrim only trims the left), or empty/trailing garbage after a number when DecimalFormat is strict.","commonSituations":"CSV/Excel imports where numbers carry currency symbols or locale-mismatched separators; fields that should be numeric but contain \"N/A\" or \"-\"; Kettle server JVM default locale differing from the developer's machine so the same string parses locally but fails on the server; trailing spaces or units (\"12 kg\").","solutions":["Clean the string before parsing: trim and strip currency symbols/units, then retry str2num.","Match separators to the JVM's default locale, or pre-normalize \"1.234,56\"-style strings manually.","Use the 2-argument form with an explicit DecimalFormat pattern matching the input format.","Check the server's -Duser.language/-Duser.country settings if parsing works locally but not on the server.","Decide a fallback for unparseable values (default 0 or NaN) with a isNaN check after a guarded call."],"exampleFix":"// before\nvar n = str2num(amountText); // \"€1.234,56 \" under en locale fails\n\n// after\nvar cleaned = trim(amountText).replace(\"€\", \"\").replace(\"\\.\", \"\").replace(\",\", \".\");\nvar n = str2num(cleaned);","handlingStrategy":"validation","validationCode":"function isParseableNumber(s) {\n  if (typeof s !== 'string') return false;\n  var t = s.replace(/^\\s+/, '').replace(/[\\s\\u00a4]/g, '');\n  return /^-?\\d+([.,]\\d+)?$/.test(t);\n}\nif (isParseableNumber(amountText)) { var n = str2num(amountText); }","typeGuard":"function isNumericString(v) { return typeof v === 'string' && !isNaN(Number(v.replace(',', '.'))); }","tryCatchPattern":null,"preventionTips":["Trim and strip currency symbols/units before parsing.","Align decimal/thousands separators with the JVM default locale or pre-normalize.","Confirm server and dev machines share the same locale settings.","Handle 'N/A'/dash placeholders upstream before conversion."],"tags":["javascript","kettle","parsing","locale","number-format"],"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"}