Skip to reference

std.fmt.decimal

wolf-std d58f9e1 · toolchain pin verified against wolf 0.2.41 (0261597). Source signatures and documentation; execution support is stated in each module.

All modules · Plain text · Stability tier: core

Module

std.fmt.decimal — the f64↔decimal-string boundary, done exactly. Stability tier: core.

What this module promises. Every finite f64 is exactly a decimal number (a 53-bit integer times a power of two is a finite decimal), so there is a right answer for every question here and this module gives it: to_str_fixed and to_str_exp round the EXACT value of the input — never a floating-point approximation of it — with round-half-even at the last kept digit, and to_str produces the SHORTEST digit string that reads back as the same bits. parse_float is correctly rounded: the returned f64 is the one nearest the exact decimal the text denotes, ties to even. The two are inverses on the round trip the sprint contract names: parse_float(to_str(x)) has to_bits equal to x's for every finite x, and tests/fmt/decimal/round_trip.lu proves it over the torture corpus rather than asserting it.

How, without a bignum type. The exactness comes from one small engine: a non-negative big integer as a List[int] of base-10^9 limbs, with the only two operations these algorithms need — multiply by a small factor, and render to digits. A double m * 2^e becomes an exact decimal by multiplying m by 2^e when e >= 0 and by 5^(-e) when e < 0 (because m / 2^k is m * 5^k / 10^k, and dividing by a power of ten in base 10^9 is just where the point goes). Everything else — rounding, shortest-digit search, and the comparisons the parser needs — is string arithmetic over ASCII digits, which is cheap and exact. The shortest search never calls the parser: it compares the candidate against the two exact MIDPOINTS of the input's rounding interval, so "round-trips" is decided by arithmetic rather than by trying.

**Why the module is not called std.fmt.float.** That is its natural name (std.math/std.math.float set the pattern in §10), and it is unavailable: the module namespace is flat at the LAST path segment at these pins, so std.fmt.float cannot import std.math.float (lupin calls it an import cycle, float → float, E0303) and cannot be imported beside it (wolfc rejects the second binding, E0306, while lupin silently keeps one of the two). Filed as F-0034. decimal is the noun for the boundary this module owns, so the name the defect forced is also the better name, and std keeps it when the defect closes.

Accuracy, stated as §10 requires. Exact, not approximate: no ulp budget appears here because none is spent. The printing side rounds the exact value once; the parsing side rounds the exact value once. The only inexactness in the module is the first ESTIMATE the parser's slow path starts from, and an estimate that is wrong by any number of ulps costs time and not accuracy — the exact comparison that follows moves it to the right answer or the guard trips and the estimate is rejected.

Not here: locale-aware anything (a non-target, forever, in core), hexadecimal float literals (0x1p-3, a lexer question), %g-style "shortest of fixed and exponential" (to_str already answers the question %g was invented for), and any rendering of a f64 that rounds to fewer digits than it takes to round-trip — that is to_str_fixed's job and it is spelled with the precision the caller asked for.

Types and traits

No public declarations in this section.

Functions and methods

to_str_fixed

fn · Source line 619

pub fn to_str_fixed(x: f64, prec: int) -> str

x rendered with exactly prec digits after the decimal point, the exact value rounded half-even at the last one — %.*f, done right. A negative prec traps assert (a caller contract violation, §2); prec == 0 renders no point at all. -0.0 keeps its sign, because the sign of zero is observable and a formatter that hides it is lying about the value it was given.

Non-finite inputs render as "nan", "inf" and "-inf" — the spellings parse_float deliberately does NOT accept, so a round trip through text is not a way to smuggle an infinity into a program.

Total otherwise: no error row, no traps beyond allocation. The cost is linear in the number of exact digits, which for a subnormal is about 750 — bounded, but not free.

decimal.to_str_fixed(1.5, 2) == "1.50"
decimal.to_str_fixed(1.0, 0) == "1"
decimal.to_str_fixed(0.5, 0) == "0"
decimal.to_str_fixed(1.5, 0) == "2"
decimal.to_str_fixed(2.5, 0) == "2"
decimal.to_str_fixed(0.1, 20) == "0.10000000000000000555"
decimal.to_str_fixed(-0.0, 1) == "-0.0"
decimal.to_str_fixed(0.0, 3) == "0.000"

to_str_exp

fn · Source line 668

pub fn to_str_exp(x: f64, prec: int) -> str

x in exponential notation with exactly prec digits after the point in the mantissa — %.*e, over the exact value, rounded half-even. The exponent carries a sign and at least two digits ("1.50e+00"), and prec == 0 renders the mantissa alone ("2e+00"). A negative prec traps assert.

Zero renders with a zero exponent ("0.00e+00"), -0.0 keeps its sign, and the non-finite spellings are to_str_fixed's.

decimal.to_str_exp(1.5, 2) == "1.50e+00"
decimal.to_str_exp(1500.0, 2) == "1.50e+03"
decimal.to_str_exp(0.015, 1) == "1.5e-02"
decimal.to_str_exp(2.0, 0) == "2e+00"
decimal.to_str_exp(0.0, 2) == "0.00e+00"
decimal.to_str_exp(9.99, 1) == "1.0e+01"

to_str

fn · Source line 757

pub fn to_str(x: f64) -> str

x as the SHORTEST decimal string that reads back as exactly x — the round-trip contract the sprint names, and the rendering ordinary code wants: to_str(0.1) is "0.1", not 0.1's exact 55-digit expansion, and parse_float maps it back to the same bits.

The digit count is genuinely minimal: the candidate is compared against the exact midpoints of x's rounding interval, and a candidate that lands exactly ON a midpoint counts when the tie goes this way (round-half-even, so when x's mantissa is even). That boundary rule is the difference between to_str(9.999999999999999e22) being "1e+23" and being seventeen digits, and it is why the shortest search must know how the READER breaks ties.

Layout: positional when the decimal exponent of the leading digit is in -4..16, scientific otherwise ("1e+17", "1e-05") — the rule every language with this function picks, with the crossover written down instead of discovered. Integral values render without a trailing ".0" (to_str(3.0) is "3"); to_str_fixed(x, 1) is the spelling that keeps one. -0.0 renders "-0", and the non-finite spellings are to_str_fixed's.

decimal.to_str(0.1) == "0.1"
decimal.to_str(1.5) == "1.5"
decimal.to_str(3.0) == "3"
decimal.to_str(100.0) == "100"
decimal.to_str(0.0) == "0"
decimal.to_str(-0.0) == "-0"
decimal.to_str(1e17) == "1e+17"
decimal.to_str(0.00001) == "1e-05"
decimal.to_str(0.3333333333333333) == "0.3333333333333333"

parse_float

fn · Source line 874

pub fn parse_float(s: str) -> f64 ! {parse, overflow}

s parsed as an f64, correctly rounded: the result is the double nearest the exact decimal value the text denotes, ties to even. parse when the text is not a number; overflow when the value rounds beyond the finite range (§10's tag, meaning here exactly what it means for integers: a result outside the type). It is NOT spelled range: a row tag that shares a name with a module in scope — and std.range is one — resolves to that MODULE and leaves the function as an ordinary value, with no diagnostic and no else firing (F-0036).

The accepted shape is an optional sign, decimal digits with an optional . and fractional digits (at least one digit in total), and an optional e/E exponent with its own optional sign and at least one digit. Nothing else: no whitespace, no digit separators, no 0x, and no inf/nan spellings — those are values a program can write and not text a caller should be able to smuggle. Non-ASCII input is a miss and never a trap (F-0018's boundary-safe probe).

Underflow is IEEE's and not an error: a value below half the smallest subnormal rounds to a signed zero, exactly as the arithmetic would. overflow IS an error, because rounding 1e400 to inf() would turn text that names no f64 into a value that looks like one.

(decimal.parse_float("1.5") else 0.0) == 1.5
(decimal.parse_float("-2.25") else 0.0) == -2.25
(decimal.parse_float("3") else 0.0) == 3.0
(decimal.parse_float("1e2") else 0.0) == 100.0
(decimal.parse_float("5e-1") else 0.0) == 0.5
(decimal.parse_float("0.1") else 0.0) == 0.1
(decimal.parse_float("1e400") else 0.0) == 0.0
(decimal.parse_float("") else -1.0) == -1.0
(decimal.parse_float("1.2.3") else -1.0) == -1.0

Constants

No public declarations in this section.

Errors and row tags

Tags named by public signatures: overflow, parse. Their conditions are documented on the functions that return them.

No public declarations in this section.