-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Precisely format large numbers using Intl through i18n #773
Conversation
Deployed to Cloudflare Pages
|
01b82a1
to
ab211c1
Compare
test.describe('getPreciseNumberFormat', () => { | ||
test('small number should be precise and formatted', async ({ page }) => { | ||
await setup(page, '111222333444555', 9) | ||
// Expect precisely formatted small number even when browser doesn't support precise formatting for large numbers | ||
await expect(page.getByText('111,222.333444555', { exact: true })).toBeVisible() | ||
}) | ||
|
||
test('large number should be precise and formatted or fallback to precise unformatted number in browsers without support', async ({ | ||
page, | ||
}) => { | ||
await setup(page, '111222333444555666777888999111222333444555666', 18) | ||
await expect( | ||
page | ||
.getByText('111,222,333,444,555,666,777,888,999.111222333444555666', { exact: true }) | ||
// Expect precise fallback when browser doesn't support precise formatting | ||
.or(page.getByText('111222333444555666777888999.111222333444555666', { exact: true })), | ||
).toBeVisible() | ||
}) | ||
|
||
test('exceeding maximumFractionDigits:20 should fallback to precise unformatted number', async ({ | ||
page, | ||
}) => { | ||
await setup(page, '111222333444555666777888999111222333444555666', 36) | ||
await expect( | ||
page.getByText('111222333.444555666777888999111222333444555666', { exact: true }), | ||
).toBeVisible() | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How it behaves across browsers
@donouwens says "Yeah, I think this should work. 👍"
"value": "Value", | ||
"valueInToken": "{{value}} {{ticker}}", | ||
"roundedValueInToken": "{{value}}… <TickerLink />", | ||
"valueInTokenWithLink": "{{value}} <TickerLink />", | ||
"valueLong": "{{value, number}}", | ||
"valueInToken": "{{value, number}} {{ticker}}", | ||
"roundedValueInToken": "{{value, number}}… <TickerLink />", | ||
"valueInTokenWithLink": "{{value, number}} <TickerLink />", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know how to better name these and lessThanAmount and valuePair. Future issue?
// Firefox 115 and Safari 16.5 return '111,222,333,444,555,670,000,000,000' | ||
.at(-1) === '6' | ||
|
||
export function getPreciseNumberFormat(value: string) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also open for name suggestions
"valueInToken": "{{value}} {{ticker}}", | ||
"roundedValueInToken": "{{value}}… <TickerLink />", | ||
"valueInTokenWithLink": "{{value}} <TickerLink />", | ||
"valueLong": "{{value, number}}", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This whole situation sucks. I think this probably the best we can do in these circumstances. Handles all corner cases nicely. LGTM.
In browsers with imprecise Intl.NumberFormat: display unformatted original string instead of implicitly being converted to rounded float.
In browsers with imprecise Intl.NumberFormat: display formatted number after allowing implicit conversion to rounded float.
Alternative to / opposite of #333
Consistently format even large stringified numbers using native Intl
TODO: replace all toLocaleString and
<span>{param.value as string}</span>