Skip to content

feat(format): show exact fractions the same way in every output - #677

Merged
LeMyst merged 2 commits into
cooklang:mainfrom
LeMyst:feat/exact-fractions
Oct 11, 2026
Merged

LeMyst merged 2 commits into
cooklang:mainfrom
LeMyst:feat/exact-fractions

Conversation

@LeMyst

@LeMyst LeMyst commented Oct 10, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #672.

The same recipe showed different numbers depending on where you looked. There were three formatters: the web pages used format_number, the shopping list page used its own copy in JavaScript, and every other output used cooklang's Display.

Mix @milk{1 5/8%cup}, @cocoa{5/8%cup} and @sugar{3/4%cup}.
before after
cook recipe (terminal, Markdown, LaTeX, Typst, schema.org) 1.625 cup, 0.625 cup, 0.75 cup 1 5/8 cup, 5/8 cup, 3/4 cup
recipe and menu pages 1.625 cup, 5/8 cup, 3/4 cup 1 5/8 cup, 5/8 cup, 3/4 cup
shopping list page 1 5/8 cup, 5/8 cup, 3/4 cup unchanged
cook shopping-list 1.625 cup, 0.625 cup, 0.75 cup 1 5/8 cup, 5/8 cup, 3/4 cup
JSON / YAML / Cooklang output 1.625 unchanged

Grams, litres, times and temperatures stay in decimals:

before after
@flour{100.625%g} on the recipe page 100.625 g 100.625 g (not 100 5/8 g)
@butter{0.5%kg} on the recipe page 1/2 kg 0.5 kg
@butter{0.5%kg} in the terminal 0.5 kg 0.5 kg

What changes

One rule, in cooklang_format::number:

  • A value that is exactly one of the fractions the web already knew (halves, thirds, quarters and eighths) is shown as that fraction. Above 1 it is a mixed number: 1 5/8, 2 1/3. Until now the Rust formatter gave up above 1 on purpose, while the shopping list's JavaScript did not.
  • Any other value stays a decimal, rounded to 3 places. Nothing is rounded to the nearest fraction, which was the bug in Inconsistent quantity display: lossy fraction approximation (1 5/8 cup shown as 1 2/3 c) and no control over unit conversion #432. For example, 5/8 scaled ×1.5 is 0.938, not 1.
  • Fractions only apply to what is measured with cups and spoons, or counted. Metric units (g, kg, mg, ml, cl, dl, l and their spelled-out names), times and °C/°F always read in decimals. This follows cooklang's own unit database ([fractions] in its units.toml: metric, time and temperature off, imperial on). No unit, cups, spoons, pounds and units not in that list (cloves, pinch) keep the fractions. A bare c or f keeps them too, because c is also a cup.
  • format_quantity_value (new) takes the quantity and applies its unit's rule. format_quantity_with_unit (new) renders 1 5/8 cup and 100.625 g.
  • grouped_quantity_fmt uses it, which covers every ingredient list.
  • So do the step text in the terminal, Markdown, LaTeX, Typst and schema.org (timers, inline quantities), the CLI shopping list, and the step labels in the picture dialog.

Ranges are still kept as text (1/2-1 cup, shown as written) because range values are off in the parser (#597). Once they are on, they will read 1/2 - 1 in every output, as the web pages already do.

Unchanged:

  • JSON and YAML keep the number, and the -f cooklang output stays valid Cooklang.
  • The shopping list page's JavaScript formatNumber already gave mixed numbers. It now has the same unit list, and both copies point at each other.
  • format_number and format_quantity still give fractions. They are for numbers without a unit, such as servings and numeric metadata.

On the questions in the issue

  • Default or opt-in: default. The web already showed fractions below 1 without a setting, so this extends that to mixed numbers and to the other outputs instead of adding a preference and a CLI flag. If you would rather have it opt-in, I can add one.
  • Scaling: a scaled value that is not one of these fractions stays a decimal (2 7/16 → 2.438).

Worth knowing

  • cooklang-format is published, and its human-readable output changes. feat: lets release-please bump it.
  • A plan written from the calendar with 2½ servings now stores @./x{2 1/2%servings} rather than 2.5. Values below 1 were already written as 1/2. I checked that a menu reads both the same.
  • Unrelated, found while checking that: a menu line with 2.5%servings puts 3 servings' worth on the shopping list, and 2 1/2 does the same; whole servings scale correctly. This happens on main too. I will file it separately if you agree it is a bug.

Tests

  • crates/format/src/number.rs:
    • mixed numbers;
    • values between fractions stay decimals (1.6, 0.33, 0.938, 2.438);
    • format_quantity_with_unit;
    • which units use fractions;
    • grams, litres, °C and hours in decimals while Tbsp, cloves, counts and cups keep fractions.
  • tests/fractions_test.rs (new, 6 tests): the issue's recipe through cook recipe (terminal and Markdown), scaled ×1.5, through cook shopping-list, and JSON keeping the number. Also grams and litres staying decimals in the recipe and the shopping list. Every cook it runs goes through with_isolated_config.
  • Updated expectations: one snapshot (*1.5 cups* milk becomes *1 1/2 cups* milk in the Markdown output) and a menu scaling unit test (1/2 × 3 is now 1 1/2).
  • Locally:
    • cargo fmt --check, cargo clippy --workspace --all-targets and cargo test --workspace are clean.
    • Playwright Chromium: 287 passed, 5 skipped, 2 failed with 2 workers. The 2 failures were both in shopping-list-copy; the shopping-list specs share .shopping-list, and they pass with one worker (18/18).
    • Checked live: Risotto ×1.125 shows 22.5 g, 112.5 ml and 1.125 l, alongside 2 1/4 tbsp.

🤖 Generated with Claude Code

LeMyst and others added 2 commits October 10, 2026 23:09
`@milk{1 5/8%cup}` read `1.625 cup` in the terminal and on the recipe page,
while `5/8 cup` read `0.625 cup` in the terminal and `5/8 cup` on the page,
and the shopping list page showed `1 5/8 cup` (cooklang#672).

Every human-readable output now goes through `cooklang_format::number`:
the terminal, Markdown, LaTeX, Typst, schema.org text, the CLI shopping list
and its JSON strings, the step labels of the picture dialog, and the web
pages, which already did. `format_number` gives a mixed number when the value
is exactly one of the fractions it knows (halves, thirds, quarters, eighths):
`1 5/8`, `2 1/3`. Any other value stays a decimal, so nothing is rounded to
the nearest fraction (the bug in cooklang#432). Ranges read `1/2 - 1` everywhere.

JSON and YAML recipe output keep the number, and the Cooklang output is
unchanged. The shopping list page's own formatter already followed these
rules; it now says so.

Closes cooklang#672

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Fractions suit cups and spoons, not a scale: `100.625 g` read
`100 5/8 g`, and the web pages already showed `0.5 kg` as `1/2 kg`.

As in cooklang's own unit database (`[fractions]` in its units.toml:
metric, time and temperature off, imperial on), metric units, times and
°C/°F now always read in decimals. Cups, spoons, pounds, counts and units
not in that list (cloves, pinch) keep the fractions. A bare `c` or `f`
stays a fraction unit, since `c` is also a cup.

`format_quantity_value` takes the quantity and applies its unit's rule;
every caller with a unit at hand uses it, and the shopping list page's
JavaScript has the same list. `format_quantity` and `format_number` still
give fractions, for numbers without a unit such as servings.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@LeMyst
LeMyst marked this pull request as ready for review October 11, 2026 07:03
@LeMyst
LeMyst merged commit c0f69f6 into cooklang:main Oct 11, 2026
6 checks passed
@LeMyst
LeMyst deleted the feat/exact-fractions branch October 11, 2026 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Show exact fractions (5/8, 1 5/8) the same way in the terminal and the web UI

1 participant