Skip to content

fix(ui): keep integer zeros in formatNumber - #1143

Merged
pedromcunha-uneven merged 2 commits into
relayprotocol:mainfrom
erkancamli:fix/ui-format-number-integer-zeros
Nov 10, 2026
Merged

pedromcunha-uneven merged 2 commits into
relayprotocol:mainfrom
erkancamli:fix/ui-format-number-integer-zeros

Conversation

@erkancamli

Copy link
Copy Markdown
Contributor

formatNumber trims trailing zeros with .replace(/\.?0+$/, ''), which also matches whole numbers because the dot is optional: 100 becomes 1, 10 becomes 1 and 1,000 becomes 1,.

Its callers are the token equivalents in the Swap Widget's USD input mode (SwapWidget/index.tsx:922 and :1340). The amount is (usdValue / conversionRate).toFixed(decimals), so typing $100 for USDC shows 1 USDC under the input, and anything that rounds to a whole number ending in 0 at 4 decimals (for example 9.99999) shows as 1.

The regex now only trims zeros after a decimal point and then drops a dangling point. Changeset included. pnpm typecheck and pnpm lint in packages/ui pass. The ui package has no test runner, so I checked the cases (100, 10, 1,000, 1.50, 2.0, 1.5K) with a throwaway vitest file that is not part of this PR.

@vercel

vercel Bot commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

@erkancamli is attempting to deploy a commit to the Uneven Labs Team on Vercel.

A member of the Team first needs to authorize it.

@greptile-apps

greptile-apps Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Low impact] The PR appears safe to merge.

Summary

formatNumber now returns the Intl.NumberFormat output directly.

  • Token amounts in USD mode keep their integer zeros.

Reviews (3) · Last reviewed commit: "fix(ui): return the Intl.NumberFormat ou..." · Reviewed by Greptile

The trailing zero regex also matched whole numbers, so 100 became 1 and 1,000 became 1, in the USD input mode token equivalent.

return format(numAmount).replace(/\.?0+$/, '')
// Only strip trailing zeros from the fraction, never from the integer part
return format(numAmount)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice catch. Intl.NumberFormat with only maximumFractionDigits set never outputs trailing zeros after the decimal point, so these two replaces don't change anything. I checked standard, compact, negative and fractional inputs, and the output is the same as raw format(). Can we drop them, and the comment, and just return format(numAmount)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point, confirmed: with only maximumFractionDigits set, Intl.NumberFormat never ends the fraction with zeros. I compared format() with and without the two replaces for standard and compact notation and maximumFractionDigits 0 to 10 over about 320k values, including negatives, and they never differed. Dropped both replaces and the comment, so it is just format(numAmount) now, and updated the changeset text.

With only maximumFractionDigits set, Intl.NumberFormat never ends the
fraction with zeros, so the two replaces had nothing left to strip.
@pedromcunha-uneven
pedromcunha-uneven merged commit cd7d6ad into relayprotocol:main Nov 10, 2026
7 of 10 checks passed
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