format-number

xod/core/format-number

Transforms a number into a string with specified precision
format-number
@/format-number
Transforms a number into a string with specified precision
NUMnumber
Number to format
DIGnumber
Number of digits after dot
format-number
NUM
DIG
STR
STRstring
Formatted value

C++ implementation

#pragma XOD dirtieness disable

node {
    char str[16];
    CStringView view = CStringView(str);

    void evaluate(Context ctx) {
        auto num = getValue<input_NUM>(ctx);
        auto dig = getValue<input_DIG>(ctx);
        formatNumber(num, dig, str);
        emitValue<output_STR>(ctx, XString(&view));
    }
}

Tabular tests

NUMDIGSTR
04"0.0000"
12"1.00"
1.32724"1.3272"
1.32722"1.33"
1.32720"1"
1.47230"1"
1.50"2"
1.5-2"2"
-1.19924"-1.1992"
-1.4492"-1.45"
-1.590"-2"
-1.59-2"-2"
-1.59-2"-2"
.2572"0.26"
.2571"0.3"
.2570"0"
-.2572"-0.26"
-.2571"-0.3"
-.2570"0"
-.6570"-1"
123.456-2"123"
-0.256-7"0"
990000000000"OVF"
990000000002"OVF"
-990000000000"-OVF"
-990000000002"-OVF"
NaN0"NaN"
Inf0"Inf"
-Inf0"-Inf"
NaN2"NaN"
Inf2"Inf"
-Inf2"-Inf"