{"record":{"id":"b4775eabc6cb6786","repo":"pentaho/pentaho-kettle","slug":"function-num2str-only-works-on-numbers-and-integers","errorCode":null,"errorMessage":"Function NUM2STR only works on Numbers and Integers","messagePattern":"Function NUM2STR only works on Numbers and Integers","errorType":"exception","errorClass":"KettleValueException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/org/pentaho/di/compatibility/Value.java","lineNumber":3082,"sourceCode":"        if ( groupingSymbol != null && groupingSymbol.length() > 0 ) {\n          dfs.setGroupingSeparator( groupingSymbol.charAt( 0 ) );\n        }\n        if ( decimalSymbol != null && decimalSymbol.length() > 0 ) {\n          dfs.setDecimalSeparator( decimalSymbol.charAt( 0 ) );\n        }\n        df.setDecimalFormatSymbols( dfs ); // in case of 4, 3 or 2\n        if ( format != null && format.length() > 0 ) {\n          df.applyPattern( format );\n        }\n        try {\n          setValue( nf.format( getNumber() ) );\n        } catch ( Exception e ) {\n          setType( VALUE_TYPE_STRING );\n          setNull();\n          throw new KettleValueException( \"Couldn't convert Number to String \" + e.toString() );\n        }\n      } else {\n        throw new KettleValueException( \"Function NUM2STR only works on Numbers and Integers\" );\n      }\n    }\n    return this;\n  }\n\n  // date: TO_CHAR( <date> , 'yyyy/mm/dd HH:mm:ss'\n  public Value dat2str() throws KettleValueException {\n    return dat2str( null, null );\n  }\n\n  public Value dat2str( String arg0 ) throws KettleValueException {\n    return dat2str( arg0, null );\n  }\n\n  public Value dat2str( String arg0, String arg1 ) throws KettleValueException {\n    if ( isNull() ) {\n      setType( VALUE_TYPE_STRING );\n    } else {","sourceCodeStart":3064,"sourceCodeEnd":3100,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/core/src/main/java/org/pentaho/di/compatibility/Value.java#L3064-L3100","documentation":"Thrown by Value.to_char() (NUM2STR) when the receiving Value is neither Number nor Integer. The function only formats numeric types through DecimalFormat; String, Date, Boolean, BigNumber, and Binary types raise KettleValueException.","triggerScenarios":"Calling value.to_char(pattern) (or to_char() with decimal/format arguments) on a Value whose type is String, Date, Boolean, or BigNumber rather than VALUE_TYPE_NUMBER or VALUE_TYPE_INTEGER.","commonSituations":"Formatting a number that was already converted to String by an earlier step; applying NUM2STR to dates (use the date TO_CHAR overload instead); BigNumber columns that this legacy path does not accept.","solutions":["Check the value type before calling: use the date overload for Date values and ensure numbers are Number/Integer.","Convert BigNumber values to Number (setValue with getNumber()) if acceptable precision-wise.","Fix field metadata upstream so the column arrives as Number/Integer.","Catch KettleValueException and fall back to String.valueOf(value.getNumber()) style conversion."],"exampleFix":"// before\nValue s = bigDecimalValue.to_char(\"#,##0.00\"); // BigNumber not accepted\n// after\nValue s = new Value(value.getName(), value.getNumber()).to_char(\"#,##0.00\");","handlingStrategy":"type-guard","validationCode":"if (value.getType() != Value.VALUE_TYPE_NUMBER && value.getType() != Value.VALUE_TYPE_INTEGER) { throw new IllegalArgumentException(\"NUM2STR requires Number or Integer\"); }","typeGuard":"boolean num2strSafe(Value v) { return v.getType() == Value.VALUE_TYPE_NUMBER || v.getType() == Value.VALUE_TYPE_INTEGER; }","tryCatchPattern":"try { result = value.to_char(pattern); } catch (KettleValueException e) { result = value.isNumeric() ? String.valueOf(value.getNumber()) : value.getString(); }","preventionTips":["Use the date TO_CHAR overload for Date values","Convert BigNumber to Number before NUM2STR if precision allows","Don't pre-convert numbers to String before formatting","Check value.getType() before formatting functions"],"tags":["kettle","type-mismatch","number-formatting"],"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"}