format-number-copy

hio/nwt-gus-hi/format-number-copy

Transforms a number into a string with specified precision
format-number-copy
@/format-number-copy
Transforms a number into a string with specified precision
NUMnumber
Number to format
DIGnumber
Number of digits after dot
format-number-copy
NUM
DIG
STR
STRstring
Formatted value
To use the node in your project you should have the hio/nwt-gus-hi library installed. Use the “File → Add Library” menu item in XOD IDE if you don’t have it yet. See Using libraries for more info.

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"