Skip to content

fix(web): stop formatUsageNumber rendering 1000.0K at unit boundaries#1340

Open
abhay-codes07 wants to merge 1 commit into
supermemoryai:mainfrom
abhay-codes07:fix/usage-number-boundary-rollover
Open

fix(web): stop formatUsageNumber rendering 1000.0K at unit boundaries#1340
abhay-codes07 wants to merge 1 commit into
supermemoryai:mainfrom
abhay-codes07:fix/usage-number-boundary-rollover

Conversation

@abhay-codes07

Copy link
Copy Markdown
Contributor

Problem

formatUsageNumber scales the value and formats it with one decimal:

if (value >= 1_000) {
  const thousands = value / 1_000
  return thousands % 1 === 0 ? `${thousands}K` : `${thousands.toFixed(1)}K`
}

toFixed(1) rounds, so any value from 999,950 to 999,999 becomes 999.95..999.999"1000.0", and the usage counter shows "1000.0K" instead of "1.0M". The same artifact appears at the million boundary (999,999,950 → "1000.0M").

Fix

When the scaled value rounds up to 1000, promote it to the next unit, and add a billions tier so very large counts read "1.0B" instead of "1000.0M":

if (value >= 1_000) {
  const thousands = value / 1_000
  return thousands >= 999.95
    ? withSuffix(value / 1_000_000, "M")
    : withSuffix(thousands, "K")
}

Tests

Added billing-utils.test.ts (bun:test) covering both boundaries: 999,950 and 999,999 → "1.0M", 999,949 → "999.9K" (still rounds within band), 999,999,950 → "1.0B", plus the existing 50K / 1.5M / 2M cases. 5 tests pass.

One-decimal rounding pushed values just below a unit over the top: 999,950
through 999,999 divided by 1,000 give 999.95..999.999, which toFixed(1) rounds
to "1000.0", so the usage counter displayed "1000.0K" instead of "1.0M". The
same happened at the million boundary ("1000.0M"). Promote to the next unit
when the scaled value rounds to 1000, and add a billions tier so large counts
render as "1.0B" rather than "1000.0M". Adds unit tests around both boundaries.
Copilot AI review requested due to automatic review settings July 22, 2026 15:14

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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.

2 participants