Skip to content

Formatter prints "80.0%" and "$10000" when rounding crosses a precision boundary #417

Closed

Description

@Borisserz

Problem

TokenFormatter.percent and TokenFormatter.costCompact pick their output precision from the raw value and only then round. When rounding crosses a boundary, the result contradicts the branch:

Call Current Expected
percent(79.96) 80.0% 80%
percent(99.97) 100.0% 100%
costCompact(99.96) $100.0 $100
costCompact(9_999.6) $10000 $10.0K

percent checks value == value.rounded(), so any non-integer goes through %.1f, even when it rounds to .0. costCompact checks usd < 100 / usd < 10_000 on the raw value, so values just below a cutoff print in the format of the next bucket.

Reachability

The inputs are arbitrary fractions in production:

  • Cursor usedPercent is fractional, and remaining mode shows 100 − x (menu bar UsageStore limit parts, popover quota rows, floating pet, notifications).
  • The usage recap delta is abs(delta) * 100.
  • Costs come from per-token prices (UsageCost.text(compact:), menu bar cost).

So the menu bar can show 5h 80.0% next to 7d 88%, or $10000 where every other value above $10K is $10.0K.

Why the tests missed it

LocalUsageCacheTests.testFormatterEdges only uses values in the middle of each bucket (88, 88.35, 9.54, 311.4). None of them rounds across a boundary.

Fix

Format with the bucket's precision first, then decide the bucket from the formatted string.

Activity

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions