pad-with-zeroes

xod/core/pad-with-zeroes

Transforms a number into a string and adds zeroes to the beginning of the string until it is W-sized. Ignores a fractional part of the value and a sign. If the width of a string for a number is greater than the specified W value, node produces a string with the untransformed number
pad-with-zeroes
@/pad-with-zeroes
Transforms a number into a string and adds zeroes to the beginning of the string until it is W-sized. Ignores a fractional part of the value and a sign. If the width of a string for a number is greater than the specified W value, node produces a string with the untransformed number
INnumber
Wnumber
pad-with-zeroes
IN
OUT
W
OUTstring

C++ implementation

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

    void evaluate(Context ctx) {
        auto val = getValue<input_IN>(ctx);
        auto num = (val == 0) ? 0 : abs(val);
        char strNum[16];
        formatNumber(num, 0, strNum);

        // If input value is NaN, Inf or OVF -> return without padding
        if (isnan(val) || isinf(val) || num > 0x7FFFFFFF) {
          strcpy(str, strNum);
          return;
        }

        int8_t lenFull = (getValue<input_W>(ctx) < 0) ? 0 : min((Number)15, getValue<input_W>(ctx));
        int8_t lenStr = strlen(strNum);
        size_t zeroCount =  max(0, lenFull - lenStr);
        memset(str, '0', zeroCount);
        strcpy(str + zeroCount, strNum);

        emitValue<output_OUT>(ctx, XString(&view));
    }
}

Tabular tests

INWOUT
9326"000932"
1.324"0001"
-1.158"00000001"
1.32720"1"
990000000004"OVF"
-990000000005"OVF"
NaN0"NaN"
Inf0"Inf"
-Inf0"Inf"
NaN5"NaN"
Inf5"Inf"
-Inf5"Inf"