Skip to content

format_number returns a positive fraction for negative numbers #418

Closed

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.

Activity

  1. added
    help wanted
    on Aug 13, 2026
  2. dubadub commented on Aug 14, 2026

    @dubadub
    MemberAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions