pad-with-spaces-int

bjbaylon/utilities-for-menus/pad-with-spaces-int

Format an integer to n number of digits, right justified.
pad-with-spaces-int
@/pad-with-spaces-int
Format an integer to n number of digits, right justified.
INnumber
Input number to format, must be an integer.
Wnumber
Number of digits in final result.
pad-with-spaces-int
IN
OUT
W
OUTstring
Formated string.
To use the node in your project you should have the bjbaylon/utilities-for-menus 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

struct State {
    char str[16];
    CStringView view;
    State()
        : view(str) {}
};

// clang-format off
{{ GENERATED_CODE }}
// clang-format on

void evaluate(Context ctx) {
    auto state = getState(ctx);

    auto val = getValue<input_IN>(ctx);
    auto num = floor(val);
    char strNum[16];
    formatNumber(num, 0, strNum);

    // If input value is NaN, Inf or OVF -> return without padding
    if (isnan(val) || isinf(val) || abs(num) > 0x7FFFFFFF) {
      strcpy(state->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(state->str, ' ', zeroCount);
    strcpy(state->str + zeroCount, strNum);

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