Skip to content

format_number returns a positive fraction for negative numbers #418

Description

@dubadub

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions