Repository navigation
fix(ui): keep integer zeros in formatNumber - #1143
pedromcunha-uneven merged 2 commits into
Conversation
|
@erkancamli is attempting to deploy a commit to the Uneven Labs Team on Vercel. A member of the Team first needs to authorize it. |
[Low impact] The PR appears safe to merge.
|
The trailing zero regex also matched whole numbers, so 100 became 1 and 1,000 became 1, in the USD input mode token equivalent.
3c34fa7 to
3f18042
Compare
|
|
||
| return format(numAmount).replace(/\.?0+$/, '') | ||
| // Only strip trailing zeros from the fraction, never from the integer part | ||
| return format(numAmount) |
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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.
formatNumbertrims trailing zeros with.replace(/\.?0+$/, ''), which also matches whole numbers because the dot is optional:100becomes1,10becomes1and1,000becomes1,.Its callers are the token equivalents in the Swap Widget's USD input mode (
SwapWidget/index.tsx:922and:1340). The amount is(usdValue / conversionRate).toFixed(decimals), so typing $100 for USDC shows1 USDCunder the input, and anything that rounds to a whole number ending in 0 at 4 decimals (for example 9.99999) shows as1.The regex now only trims zeros after a decimal point and then drops a dangling point. Changeset included.
pnpm typecheckandpnpm lintinpackages/uipass. 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.