Skip to content

StrFromF64

StrFromF64

Description

Convert a double to string

Parameters

Name Direction Description
str out String to store the result in
value in Value to convert
config in Formatting configuration (NULL for 6 decimal places)

Success

Returns str

Failure

Returns NULL if config is invalid

Usage example (Cross-references)

Usage examples (Cross-references)
                   .uppercase = (fmt_info->flags & FMT_FLAG_CAPS) != 0
            };
            StrFromF64(&temp, *v, &config);
    
            // Merge the formatted number into output
    }
    
    Str *StrFromF64(Str *str, f64 value, const StrFloatFormat *config) {
        ValidateStr(str);
    // Test StrFromF64 function
    bool test_str_from_f64(void) {
        WriteFmt("Testing StrFromF64\n");
    
        Str s = StrInit();
        // Test integer conversion
        StrFloatFormat config = {.precision = 2, .force_sci = false, .uppercase = false};
        StrFromF64(&s, 123.0, &config);
        bool result = (ZstrCompare(s.data, "123.00") == 0);
        if (!result) {
        StrClear(&s);
        config = (StrFloatFormat) {.precision = 3, .force_sci = false, .uppercase = false};
        StrFromF64(&s, 123.456, &config);
        result = result && (ZstrCompare(s.data, "123.456") == 0);
        if (!result) {
        StrClear(&s);
        config = (StrFloatFormat) {.precision = 3, .force_sci = false, .uppercase = false};
        StrFromF64(&s, -123.456, &config);
        result = result && (ZstrCompare(s.data, "-123.456") == 0);
        StrClear(&s);
        config = (StrFloatFormat) {.precision = 3, .force_sci = true, .uppercase = false};
        StrFromF64(&s, 123.456, &config);
        result = result && (ZstrCompare(s.data, "1.235e+02") == 0);
        StrClear(&s);
        config = (StrFloatFormat) {.precision = 3, .force_sci = true, .uppercase = true};
        StrFromF64(&s, 123.456, &config);
        result = result && (ZstrCompare(s.data, "1.235E+02") == 0);
        StrClear(&s);
        config = (StrFloatFormat) {.precision = 3, .force_sci = false, .uppercase = false};
        StrFromF64(&s, 0.0000123, &config);
        result = result && (ZstrCompare(s.data, "1.230e-05") == 0);
        StrClear(&s);
        config = (StrFloatFormat) {.precision = 2, .force_sci = false, .uppercase = false};
        StrFromF64(&s, 1234567890123.0, &config);
        result = result && (ZstrCompare(s.data, "1.23e+12") == 0);
        StrClear(&s);
        config = (StrFloatFormat) {.precision = 2, .force_sci = false, .uppercase = false};
        StrFromF64(&s, 0.0, &config);
        result = result && (ZstrCompare(s.data, "0.00") == 0);
        StrClear(&s);
        config = (StrFloatFormat) {.precision = 2, .force_sci = false, .uppercase = false};
        StrFromF64(&s, INFINITY, &config);
        result = result && (ZstrCompare(s.data, "inf") == 0);
        StrClear(&s);
        config = (StrFloatFormat) {.precision = 2, .force_sci = false, .uppercase = false};
        StrFromF64(&s, -INFINITY, &config);
        result = result && (ZstrCompare(s.data, "-inf") == 0);
        StrClear(&s);
        config = (StrFloatFormat) {.precision = 2, .force_sci = false, .uppercase = false};
        StrFromF64(&s, NAN, &config);
        result = result && (ZstrCompare(s.data, "nan") == 0);
    
                StrFloatFormat config = {.precision = precision, .force_sci = false, .uppercase = false};
                StrFromF64(&s, f64_values[i], &config);
                f64  recovered_f64 = 0.0;
                bool success       = StrToF64(&s, &recovered_f64, NULL);
        StrClear(&s);
        StrFloatFormat fconfig = {.precision = 3, .force_sci = false, .uppercase = false};
        StrFromF64(&s, 1e-300, &fconfig);
        f64 recovered_small = 0.0;
        success             = StrToF64(&s, &recovered_small, NULL);
        StrClear(&s);
        fconfig = (StrFloatFormat) {.precision = 3, .force_sci = false, .uppercase = false};
        StrFromF64(&s, 1e300, &fconfig);
        f64 recovered_large = 0.0;
        success             = StrToF64(&s, &recovered_large, NULL);
    
            StrFloatFormat config = {.precision = precision, .force_sci = false, .uppercase = false};
            StrFromF64(&s, test_value, &config);
    
            // String should have expected decimal places
            // Force scientific notation
            StrFloatFormat config = {.precision = 3, .force_sci = true, .uppercase = false};
            StrFromF64(&s, sci_values[i], &config);
            bool has_e = (strchr(s.data, 'e') != NULL);
            result     = result && has_e;
            StrClear(&s);
            config = (StrFloatFormat) {.precision = 3, .force_sci = true, .uppercase = true};
            StrFromF64(&s, sci_values[i], &config);
            bool has_E = (strchr(s.data, 'E') != NULL);
            result     = result && has_E;
                Str            s      = StrInit();
                StrFloatFormat config = {.precision = 6, .force_sci = false, .uppercase = false};
                StrFromF64(&s, test_value, &config);
    
                f64  recovered = 0.0;
        // Test StrFromF64 with NULL pointer - should abort
        StrFloatFormat config = {.precision = 2, .force_sci = false, .uppercase = false};
        StrFromF64(NULL, 123.45, &config);
    
        return false;
Last updated on