Skip to content

fix(frontend): promote token count display at unit boundaries - #689

Merged
junhoyeo merged 2 commits into
junhoyeo:mainfrom
thedavidweng:fix/format-token-count-boundary
Jun 8, 2026
Merged

fix(frontend): promote token count display at unit boundaries#689
junhoyeo merged 2 commits into
junhoyeo:mainfrom
thedavidweng:fix/format-token-count-boundary

Conversation

@thedavidweng

@thedavidweng thedavidweng commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Problem

formatTokenCount in packages/frontend/src/lib/utils.ts displays token counts in compact form (K/M/B/T) using toFixed(1) after dividing by the unit. When a value is near a unit boundary, toFixed(1) rounds up to "1000.0", producing displays like:

  • 999,950"1000.0K" (should be "1.0M")
  • 999,999,500"1000.0M" (should be "1.0B")

This is the root cause of #474 — tokens on the profile page appearing "rounded up" with the wrong unit.

Fix

Before formatting, check if the divided value is ≥ 999.95 (the threshold where toFixed(1) rounds to "1000.0"). When it is, promote to the next unit:

if (count >= 1_000_000) {
  const val = count / 1_000_000;
  return val >= 999.95
    ? `${(val / 1000).toFixed(1)}B`   // promote: 999.95M → 1.0B
    : `${val.toFixed(1)}M`;
}

Same pattern for K→M and B→T boundaries.

Verification

New test file __tests__/lib/formatTokenCount.test.ts covers:

  • Normal formatting at each unit (K/M/B/T)
  • Boundary promotion: 999,950 → "1.0M", 999,999,500 → "1.0B", 999,999,999,500 → "1.0T"
  • Values below the boundary remain unchanged

Closes #474


Summary by cubic

Fixes token count rounding near unit boundaries in formatTokenCount so we don’t display "1000.0K/M/B". Values that would round up are promoted to the next unit (e.g., 999,950 → 1.0M); fixes #474.

  • Bug Fixes
    • Promote when divided value ≥ 999.95 for K→M, M→B, and B→T; otherwise keep 1-decimal format.
    • Added packages/frontend/__tests__/lib/formatTokenCount.test.ts covering normal and boundary cases.

Written for commit d3d655b. Summary will update on new commits.

Review in cubic

formatTokenCount used toFixed(1) after dividing by the unit, which
rounds 999.95+ to '1000.0'.  A count of 999,950 displayed as
'1000.0K' instead of '1.0M'; 999,999,500 as '1000.0M' instead of
'1.0B'.

Check if the divided value >= 999.95 before formatting and promote to
the next unit when it does.

Closes junhoyeo#474
@vercel

vercel Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
tokscale Ready Ready Preview, Comment Jun 8, 2026 10:49am

Request Review

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 2 files

Re-trigger cubic

@thedavidweng

Copy link
Copy Markdown
Contributor Author

Frontend CI failing due to PR #685

@junhoyeo junhoyeo left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks!

@junhoyeo
junhoyeo merged commit 4cba9e8 into junhoyeo:main Jun 8, 2026
6 checks passed
@thedavidweng
thedavidweng deleted the fix/format-token-count-boundary branch June 8, 2026 21:30
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.

tokens on profile are rounded up

2 participants