Skip to content
StrFloatFormat

StrFloatFormat

StrFloatFormat

Description

Configuration for float formatting

Usage example (Cross-references)

Usage examples (Cross-references)
            // Use StrFromF64 directly with the appropriate parameters
            u8             precision = fmt_info->flags & FMT_FLAG_HAS_PRECISION ? fmt_info->precision : 6;
            StrFloatFormat config    = {
                   .precision = precision,
                   .force_sci = (fmt_info->flags & FMT_FLAG_SCIENTIFIC) != 0,
    }
    
    Str *StrFromF64(Str *str, f64 value, const StrFloatFormat *config) {
        ValidateStr(str);
        {.base = 10, .uppercase = false, .use_prefix = false, .pad_zeros = false, .min_width = 0};
    
    const StrFloatFormat STR_FLOAT_DEFAULT =
        {.precision = 6, .force_sci = false, .uppercase = false, .trim_zeros = false, .always_sign = false};
    
        // 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);
        // Test fractional conversion
        StrClear(&s);
        config = (StrFloatFormat) {.precision = 3, .force_sci = false, .uppercase = false};
        StrFromF64(&s, 123.456, &config);
        result = result && (ZstrCompare(s.data, "123.456") == 0);
        // Test negative number
        StrClear(&s);
        config = (StrFloatFormat) {.precision = 3, .force_sci = false, .uppercase = false};
        StrFromF64(&s, -123.456, &config);
        result = result && (ZstrCompare(s.data, "-123.456") == 0);
        // Test scientific notation (forced)
        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);
        // Test scientific notation (uppercase)
        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);
        // Test very small number (auto scientific notation)
        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);
        // Test very large number (auto scientific notation)
        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);
        // Test zero
        StrClear(&s);
        config = (StrFloatFormat) {.precision = 2, .force_sci = false, .uppercase = false};
        StrFromF64(&s, 0.0, &config);
        result = result && (ZstrCompare(s.data, "0.00") == 0);
        // Test infinity
        StrClear(&s);
        config = (StrFloatFormat) {.precision = 2, .force_sci = false, .uppercase = false};
        StrFromF64(&s, INFINITY, &config);
        result = result && (ZstrCompare(s.data, "inf") == 0);
        // Test negative infinity
        StrClear(&s);
        config = (StrFloatFormat) {.precision = 2, .force_sci = false, .uppercase = false};
        StrFromF64(&s, -INFINITY, &config);
        result = result && (ZstrCompare(s.data, "-inf") == 0);
        // Test NaN
        StrClear(&s);
        config = (StrFloatFormat) {.precision = 2, .force_sci = false, .uppercase = false};
        StrFromF64(&s, NAN, &config);
        result = result && (ZstrCompare(s.data, "nan") == 0);
                Str s = StrInit();
    
                StrFloatFormat config = {.precision = precision, .force_sci = false, .uppercase = false};
                StrFromF64(&s, f64_values[i], &config);
                f64  recovered_f64 = 0.0;
        // Test very small floating point
        StrClear(&s);
        StrFloatFormat fconfig = {.precision = 3, .force_sci = false, .uppercase = false};
        StrFromF64(&s, 1e-300, &fconfig);
        f64 recovered_small = 0.0;
        // Test very large floating point
        StrClear(&s);
        fconfig = (StrFloatFormat) {.precision = 3, .force_sci = false, .uppercase = false};
        StrFromF64(&s, 1e300, &fconfig);
        f64 recovered_large = 0.0;
            Str s = StrInit();
    
            StrFloatFormat config = {.precision = precision, .force_sci = false, .uppercase = false};
            StrFromF64(&s, test_value, &config);
    
            // 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);
            // Test uppercase 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);
    
                Str            s      = StrInit();
                StrFloatFormat config = {.precision = 6, .force_sci = false, .uppercase = false};
                StrFromF64(&s, test_value, &config);
    
        // Test StrFromF64 with NULL pointer - should abort
        StrFloatFormat config = {.precision = 2, .force_sci = false, .uppercase = false};
        StrFromF64(NULL, 123.45, &config);
            bool trim_zeros;  ///< Remove trailing zeros
            bool always_sign; ///< Always show + for positive numbers
        } StrFloatFormat;
    
        /// Configuration for parsing
        /// Default configurations
        extern const StrIntFormat   STR_INT_DEFAULT;
        extern const StrFloatFormat STR_FLOAT_DEFAULT;
        extern const StrParseConfig STR_PARSE_DEFAULT;
        /// FAILURE : Returns NULL if config is invalid
        ///
        Str *StrFromF64(Str *str, f64 value, const StrFloatFormat *config);
    
        ///
Last updated on