StrFloatFormat
Description
Configuration for float formatting
Usage example (Cross-references)
Usage examples (Cross-references)
- In
Convert.h:32:
bool trim_zeros; ///< Remove trailing zeros
bool always_sign; ///< Always show + for positive numbers
} StrFloatFormat;
/// Configuration for parsing
- In
Convert.h:43:
/// Default configurations
extern const StrIntFormat STR_INT_DEFAULT;
extern const StrFloatFormat STR_FLOAT_DEFAULT;
extern const StrParseConfig STR_PARSE_DEFAULT;- In
Convert.h:95:
/// TAGS: Str, Convert, F64
///
Str *StrFromF64(Str *str, f64 value, const StrFloatFormat *config);
///
- In
Io.c:2190:
// self-contained slice it can measure.
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,- In
Str.c:660:
}
Str *StrFromF64(Str *str, f64 value, const StrFloatFormat *config) {
ValidateStr(str);- In
Str.c:1162:
{.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};- In
Convert.c:163:
// Test integer conversion
StrFloatFormat config = {.precision = 2, .force_sci = false, .uppercase = false};
StrFromF64(&s, 123.0, &config);
bool result = (ZstrCompare(StrBegin(&s), "123.00") == 0);- In
Convert.c:172:
// Test fractional conversion
StrClear(&s);
config = (StrFloatFormat) {.precision = 3, .force_sci = false, .uppercase = false};
StrFromF64(&s, 123.456, &config);
result = result && (ZstrCompare(StrBegin(&s), "123.456") == 0);- In
Convert.c:181:
// Test negative number
StrClear(&s);
config = (StrFloatFormat) {.precision = 3, .force_sci = false, .uppercase = false};
StrFromF64(&s, -123.456, &config);
result = result && (ZstrCompare(StrBegin(&s), "-123.456") == 0);- In
Convert.c:187:
// Test scientific notation (forced)
StrClear(&s);
config = (StrFloatFormat) {.precision = 3, .force_sci = true, .uppercase = false};
StrFromF64(&s, 123.456, &config);
result = result && (ZstrCompare(StrBegin(&s), "1.235e+02") == 0);- In
Convert.c:193:
// Test scientific notation (uppercase)
StrClear(&s);
config = (StrFloatFormat) {.precision = 3, .force_sci = true, .uppercase = true};
StrFromF64(&s, 123.456, &config);
result = result && (ZstrCompare(StrBegin(&s), "1.235E+02") == 0);- In
Convert.c:199:
// 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(StrBegin(&s), "1.230e-05") == 0);- In
Convert.c:205:
// 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(StrBegin(&s), "1.23e+12") == 0);- In
Convert.c:211:
// Test zero
StrClear(&s);
config = (StrFloatFormat) {.precision = 2, .force_sci = false, .uppercase = false};
StrFromF64(&s, 0.0, &config);
result = result && (ZstrCompare(StrBegin(&s), "0.00") == 0);- In
Convert.c:217:
// Test infinity
StrClear(&s);
config = (StrFloatFormat) {.precision = 2, .force_sci = false, .uppercase = false};
StrFromF64(&s, F64_INFINITY, &config);
result = result && (ZstrCompare(StrBegin(&s), "inf") == 0);- In
Convert.c:223:
// Test negative infinity
StrClear(&s);
config = (StrFloatFormat) {.precision = 2, .force_sci = false, .uppercase = false};
StrFromF64(&s, -F64_INFINITY, &config);
result = result && (ZstrCompare(StrBegin(&s), "-inf") == 0);- In
Convert.c:229:
// Test NaN
StrClear(&s);
config = (StrFloatFormat) {.precision = 2, .force_sci = false, .uppercase = false};
StrFromF64(&s, F64_NAN, &config);
result = result && (ZstrCompare(StrBegin(&s), "nan") == 0);- In
Convert.c:475:
Str s = StrInit(&alloc);
StrFloatFormat config = {.precision = precision, .force_sci = false, .uppercase = false};
StrFromF64(&s, f64_values[i], &config);
f64 recovered_f64 = 0.0;- In
Convert.c:537:
// 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;- In
Convert.c:545:
// 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;- In
Convert.c:594:
Str s = StrInit(&alloc);
StrFloatFormat config = {.precision = precision, .force_sci = false, .uppercase = false};
StrFromF64(&s, test_value, &config);- In
Convert.c:615:
// Force scientific notation
StrFloatFormat config = {.precision = 3, .force_sci = true, .uppercase = false};
StrFromF64(&s, sci_values[i], &config);
bool has_e = (ZstrFindChar(StrBegin(&s), 'e') != NULL);- In
Convert.c:622:
// Test uppercase E
StrClear(&s);
config = (StrFloatFormat) {.precision = 3, .force_sci = true, .uppercase = true};
StrFromF64(&s, sci_values[i], &config);
bool has_E = (ZstrFindChar(StrBegin(&s), 'E') != NULL);- In
Convert.c:755:
Str s = StrInit(&alloc);
StrFloatFormat config = {.precision = 6, .force_sci = false, .uppercase = false};
StrFromF64(&s, test_value, &config);- In
Convert.c:818:
// Test StrFromF64 with NULL pointer - should abort
StrFloatFormat config = {.precision = 2, .force_sci = false, .uppercase = false};
StrFromF64(NULL, 123.45, &config);- In
Convert.c:1326:
Str s = StrInit(&alloc);
StrFloatFormat config = {.precision = 2, .force_sci = false, .uppercase = false};
StrFromF64(&s, F64_NAN, &config);- In
Convert.c:1345:
Str s = StrInit(&alloc);
StrFloatFormat config = {.precision = 2, .force_sci = false, .uppercase = false};
StrFromF64(&s, F64_INFINITY, &config);- In
Convert.c:1365:
Str s = StrInit(&alloc);
StrFloatFormat config = {.precision = 6, .force_sci = false, .uppercase = false};
StrFromF64(&s, 0.0001, &config);- In
Convert.c:1385:
Str s = StrInit(&alloc);
StrFloatFormat config = {.precision = 2, .force_sci = false, .uppercase = false};
StrFromF64(&s, 1e7, &config);- In
Convert.c:1405:
Str s = StrInit(&alloc);
StrFloatFormat config = {.precision = 0, .force_sci = true, .uppercase = false};
StrFromF64(&s, 5.0, &config);- In
Convert.c:1424:
Str s = StrInit(&alloc);
StrFloatFormat config = {.precision = 2, .force_sci = true, .uppercase = false};
StrFromF64(&s, 5.0, &config);- In
Convert.c:1445:
Str s = StrInit(&alloc);
StrFloatFormat config = {.precision = 2, .force_sci = true, .uppercase = false};
Str *r = StrFromF64(&s, 123.456, &config);- In
Convert.c:1465:
Str s = StrInit(&alloc);
StrFloatFormat config = {.precision = 2, .force_sci = false, .uppercase = false};
Str *r = StrFromF64(&s, 123.0, &config);- In
Convert.c:1485:
Str s = StrInit(&alloc);
StrFloatFormat config = {.precision = 0, .force_sci = false, .uppercase = false};
StrFromF64(&s, 123.0, &config);- In
Convert.c:1505:
Str s = StrInit(&alloc);
StrFloatFormat config = {.precision = 3, .force_sci = false, .uppercase = false};
StrFromF64(&s, 123.456, &config);- In
Convert.c:1526:
Str s = StrInit(&alloc);
StrFloatFormat config = {.precision = 3, .force_sci = false, .uppercase = false};
StrFromF64(&s, 0.1235, &config);- In
Convert.c:1542:
static bool test_from_f64_null_aborts(void) {
WriteFmt("Testing StrFromF64 NULL aborts\n");
StrFloatFormat config = {.precision = 2, .force_sci = false, .uppercase = false};
StrFromF64(NULL, 1.0, &config);
return false;- In
Convert.c:1549:
static bool test_from_f64_null_aborts_high_precision(void) {
WriteFmt("Testing StrFromF64 NULL aborts before the precision guard\n");
StrFloatFormat config = {.precision = 18, .force_sci = false, .uppercase = false};
StrFromF64(NULL, 1.0, &config);
return false;- In
Convert.c:1885:
Str s = StrInit(&alloc);
StrFloatFormat config = {.precision = 1, .force_sci = false, .uppercase = false};
StrFromF64(&s, 2.25, &config);
bool result = (ZstrCompare(StrBegin(&s), "2.3") == 0);- In
Convert.c:1905:
Str s = StrInit(&alloc);
StrFloatFormat config = {.precision = 1, .force_sci = false, .uppercase = false};
StrFromF64(&s, 2.25, &config);
bool result = (ZstrCompare(StrBegin(&s), "2.3") == 0);- In
Convert.c:2015:
Str s = StrInit(&bp);
StrFloatFormat config = {.precision = 0, .force_sci = false, .uppercase = false};
// Positive 15-digit value, precision 0 -> the integer digits are the only
// emit (no sign/prefix/'.'/'e' pushes). The Str is a Vec(char) with stride
- In
Convert.c:2057:
Str s = StrInit(&bp);
StrFloatFormat config = {.precision = 2, .force_sci = true, .uppercase = false};
Str *ret = StrFromF64(&s, 1.23e150, &config);
Last updated on