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)
        bool ok = FloatToInt(&result, &value);
    
        Str  text  = IntToStr(&result);
        bool right = ok && (ZstrCompare(StrBegin(&text), "5") == 0);
        bool ok = FloatToInt(&result, &value);
    
        Str  text  = IntToStr(&result);
        bool right = ok && (ZstrCompare(StrBegin(&text), "120") == 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;
    
        bool ok = int_div_mod_u64(&quotient, &remainder, &dividend, 97u);
        qtext   = IntToStr(&quotient);
    
        bool result = ok;
    
        bool ok = int_div_mod_i64(&quotient, &remainder, &dividend, (i64)13);
        qtext   = IntToStr(&quotient);
    
        bool result = ok;
        bool ok = int_div_mod(&quotient, &remainder, &dividend, &divisor);
    
        Str qtext = IntToStr(&quotient);
        Str rtext = IntToStr(&remainder);
    
        Str qtext = IntToStr(&quotient);
        Str rtext = IntToStr(&remainder);
    
        bool result = ok;
        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);
        Zstr           z   = "12345";
        StrReadFmt(z, "{}", v);
        Str  t  = IntToStr(&v);
        bool ok = (ZstrCompare(StrBegin(&t), "12345") == 0);
        StrDeinit(&t);
        Zstr             z     = "+99";
        StrReadFmt(z, "{}", v);
        Str  t  = IntToStr(&v);
        bool ok = (ZstrCompare(StrBegin(&t), "99") == 0);
        StrDeinit(&t);
        Zstr             z     = "ff";
        StrReadFmt(z, "{x}", v);
        Str  t  = IntToStr(&v);
        bool ok = (ZstrCompare(StrBegin(&t), "255") == 0);
        StrDeinit(&t);
        // Real: '_' rejected -> returns start (==z) -> str_read_fmt sees next==in ->
        // NULL; v unchanged (still 777).
        Str  t  = IntToStr(&v);
        bool ok = (out == NULL) && (ZstrCompare(StrBegin(&t), "777") == 0);
        StrDeinit(&t);
Last updated on