Skip to content

IntToStr

Description

Convert an integer to a decimal string. Two forms via argument count:

  • IntToStr(value) - uses value’s allocator.
  • IntToStr(value, alloc) - uses the explicit allocator.

Success

Returns a Str holding the base-10 textual form of value.

Failure

Returns an empty Str bound to alloc when the underlying IntTryToStr fails (intermediate allocation failure); the caller cannot distinguish that from a true empty result, so callers that need to detect failure should use IntTryToStr directly.

Usage example (Cross-references)

Usage examples (Cross-references)
        Zstr digits = "123456789012345678901234567890";
        Int  value  = IntFromStr(digits, ALLOCATOR_OF(&alloc));
        Str  text   = IntToStr(&value);
    
        bool result = ZstrCompare(StrBegin(&text), digits) == 0;
        z = "123456789012345678901234567890";
        StrReadFmt(z, "{}", dec);
        dec_text = IntToStr(&dec);
        success  = success && (ZstrCompare(StrBegin(&dec_text), "123456789012345678901234567890") == 0);
    
        bool result = FloatToInt(&result_value, &value);
        text        = IntToStr(&result_value);
        result      = result && (ZstrCompare(StrBegin(&text), "12345") == 0);
    
        IntAdd(&result_value, &huge, 10);
        text   = IntToStr(&result_value);
        result = result && (ZstrCompare(StrBegin(&text), "123456789012345678901234567900") == 0);
    
        result = result && IntSub(&result_value, &huge, 90);
        text   = IntToStr(&result_value);
        result = result && (ZstrCompare(StrBegin(&text), "12345678901234567800") == 0);
    
        IntMul(&result_value, &value, 25u);
        text = IntToStr(&result_value);
    
        bool result = ZstrCompare(StrBegin(&text), "308641972530864197250") == 0;
    
        IntPow(&result_value, &base, 20u);
        text        = IntToStr(&result_value);
        bool result = ZstrCompare(StrBegin(&text), "79792266297612001") == 0;
        StrDeinit(&text);
        IntPow(&result_value, &base, &exponent);
        text   = IntToStr(&result_value);
        result = result && (ZstrCompare(StrBegin(&text), "79792266297612001") == 0);
    
        IntDivMod(&quotient, &remainder, &dividend, 97u);
        qtext = IntToStr(&quotient);
    
        bool result = ZstrCompare(StrBegin(&qtext), "127275040218913071") == 0;
    
        bool result = IntDivExact(&result_value, &dividend, 90u);
        text        = IntToStr(&result_value);
        result      = result && (ZstrCompare(StrBegin(&text), "137174210013717421") == 0);
    
        IntDivMod(&quotient, &remainder, &dividend, 97);
        text = IntToStr(&quotient);
    
        bool result = ZstrCompare(StrBegin(&text), "127275040218913071") == 0;
    
        bool ok = IntNextPrime(&next, &value);
        text    = IntToStr(&next);
    
        bool result = ok && ZstrCompare(StrBegin(&text), "1000000007") == 0;
Last updated on