You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
format_number returns a positive fraction for negative numbers #418
format_number loses both the sign and the magnitude of negative values, returning a positive fraction instead.
Reproduction
Input
Returns
Expected
-0.5
"1/2"
"-0.5" or "-1/2"
-1.5
"1/2"
"-1.5"
-0.25
"3/4"
"-0.25"
-0.25 returning "3/4" is the clearest case: neither the sign nor the value survives.
Cause
decimal_to_fraction computes the fractional part as value - value.floor(). For -0.25 that is -0.25 - (-1.0) == 0.75, which matches the 3/4 entry in the common-fractions table. The whole > 0.0 guard that would otherwise prepend the integer part never fires, because whole is negative — so the whole part is dropped entirely.
Impact
Low in the CLI today: no seed or test recipe carries a negative quantity, and the function is reached through format_quantity for recipe and web output. It matters more as the function becomes reachable from a library API, where callers are not constrained to the CLI's input range.
Notes
A fix is in flight as part of extracting the formatters into a library crate — guarding decimal_to_fraction so negatives fall through to the decimal path, with tests pinning the corrected values. Verified to change no existing snapshot. Filing separately because it is a pre-existing defect on main.
Fixed on main by the cookcli-core extraction (#434), rather than by a change filed against this issue.
decimal_to_fraction now returns None for negatives, so they fall through to the decimal path instead of matching a common fraction off a floor-derived fractional part:
Input
Returns
-0.5
"-0.5"
-1.5
"-1.5"
-0.25
"-0.25"
Positives are untouched: 0.5 → "1/2", 0.25 → "1/4".
Verified by running the table from this issue while clearing the help wanted backlog in #444; no change was needed.
Activity
dubadub commentedon Aug 14, 2026
Fixed on
mainby thecookcli-coreextraction (#434), rather than by a change filed against this issue.decimal_to_fractionnow returnsNonefor negatives, so they fall through to the decimal path instead of matching a common fraction off afloor-derived fractional part:-0.5"-0.5"-1.5"-1.5"-0.25"-0.25"Positives are untouched:
0.5→"1/2",0.25→"1/4".Verified by running the table from this issue while clearing the
help wantedbacklog in #444; no change was needed.