{"record":{"id":"3db9a2f893080e2e","repo":"pentaho/pentaho-kettle","slug":"the-first-argument-must-be-a-number-3db9a2","errorCode":null,"errorMessage":"The first Argument must be a Number.","messagePattern":"The first Argument must be a Number\\.","errorType":"exception","errorClass":"JavaScriptException","httpStatus":null,"severity":"error","filePath":"engine/src/main/java/org/pentaho/di/trans/steps/scriptvalues_mod/ScriptValuesAddedFunctions.java","lineNumber":1344,"sourceCode":"  }\n\n  // Converts the given Numeric to a JScript String\n  public static String num2str( Context actualContext, Scriptable actualObject, Object[] ArgList,\n    Function FunctionContext ) {\n    String sRC = \"\";\n    switch ( ArgList.length ) {\n      case 0:\n        throw Context.reportRuntimeError( \"The function call num2str requires at least 1 argument.\" );\n      case 1:\n        try {\n          if ( isNull( ArgList[0] ) ) {\n            return null;\n          } else if ( isUndefined( ArgList[0] ) ) {\n            return (String) Context.getUndefinedValue();\n          }\n          double sArg1 = Context.toNumber( ArgList[0] );\n          if ( Double.isNaN( sArg1 ) ) {\n            throw Context.reportRuntimeError( \"The first Argument must be a Number.\" );\n          }\n          DecimalFormat formatter = new DecimalFormat();\n          sRC = formatter.format( sArg1 );\n        } catch ( IllegalArgumentException e ) {\n          throw Context.reportRuntimeError( \"Could not apply the given format on the number : \" + e.getMessage() );\n        }\n        break;\n      case 2:\n        try {\n          if ( isNull( ArgList, new int[] { 0, 1 } ) ) {\n            return null;\n          } else if ( isUndefined( ArgList, new int[] { 0, 1 } ) ) {\n            return (String) Context.getUndefinedValue();\n          }\n          double sArg1 = Context.toNumber( ArgList[0] );\n          if ( Double.isNaN( sArg1 ) ) {\n            throw Context.reportRuntimeError( \"The first Argument must be a Number.\" );\n          }","sourceCodeStart":1326,"sourceCodeEnd":1362,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/engine/src/main/java/org/pentaho/di/trans/steps/scriptvalues_mod/ScriptValuesAddedFunctions.java#L1326-L1362","documentation":"In the 1-argument form of num2str, the function converts the first argument with Context.toNumber and throws this error if the result is NaN. It means the value passed cannot be interpreted as a number, so no DecimalFormat formatting is possible.","triggerScenarios":"num2str(x) where x is a non-numeric string (e.g. num2str(\"abc\")), an object without numeric coercion, or a value that Rhino converts to NaN.","commonSituations":"Passing a String-typed field from the row that actually contains text; reading a field that upstream produced as alphanumeric; locale-formatted numbers with thousands separators that Rhino can't parse as a number; undefined field names resolving to odd values.","solutions":["Ensure the first argument is numeric: use str2num() first if it is a string, e.g. num2str(str2num(s)).","Verify the field's data type in the transformation (set the field to Number instead of String in the step's field mapping).","Check for NaN upstream: log or test isNaN(value) before formatting.","Strip non-numeric characters or fix the locale-specific formatting of the source string before conversion."],"exampleFix":"// before\nvar s = num2str(amountStr); // amountStr is a String field\n\n// after\nvar s = num2str(str2num(amountStr));","handlingStrategy":"type-guard","validationCode":"if (typeof value === 'number' && !isNaN(value)) { var s = num2str(value); }","typeGuard":"function isNumber(v) { return typeof v === 'number' && !isNaN(v); }","tryCatchPattern":null,"preventionTips":["Keep numeric fields typed as Number in the step's field mapping, not String.","Convert strings with str2num before formatting.","Check the transformation preview to confirm field data types."],"tags":["javascript","kettle","type-mismatch","number-format"],"backgroundTag":"type-mismatch","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"}