Skip to content
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

Merged
merged 6 commits into from
Aug 8, 2023

Conversation

lukaw3d
Copy link
Member

@lukaw3d lukaw3d commented Jul 27, 2023

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>

@github-actions
Copy link

github-actions bot commented Jul 27, 2023

Deployed to Cloudflare Pages

Latest commit: c2a618b378144bb473b2391fe338226d91217d24
Status:✅ Deploy successful!
Preview URL: https://1bf01227.oasis-explorer.pages.dev

@lukaw3d lukaw3d force-pushed the lw/format-intl branch 3 times, most recently from 01b82a1 to ab211c1 Compare August 3, 2023 18:43
Comment on lines +53 to +80
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()
})
})
Copy link
Member Author

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. 👍"

Comment on lines 97 to +101
"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 />",
Copy link
Member Author

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) {
Copy link
Member Author

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

@lukaw3d lukaw3d requested review from buberdds, csillag and lubej August 3, 2023 18:53
"valueInToken": "{{value}} {{ticker}}",
"roundedValueInToken": "{{value}}… <TickerLink />",
"valueInTokenWithLink": "{{value}} <TickerLink />",
"valueLong": "{{value, number}}",
Copy link
Member Author

Choose a reason for hiding this comment

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

Here's what happens if we add a language switcher:

English French
localhost_1234_mainnet_sapphire_address_0xE8D562606F35CB14dA3E8faB1174F9B5AE8319c4_tokens_erc-20 (1) localhost_1234_mainnet_sapphire_address_0xE8D562606F35CB14dA3E8faB1174F9B5AE8319c4_tokens_erc-20

@lukaw3d lukaw3d changed the title Format valueInToken with Intl Format large numbers using Intl through i18n Aug 3, 2023
@lukaw3d lukaw3d changed the title Format large numbers using Intl through i18n Precisely format large numbers using Intl through i18n Aug 3, 2023
Copy link
Contributor

@csillag csillag left a 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.

lukaw3d added 6 commits August 8, 2023 18:54
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.
@lukaw3d lukaw3d enabled auto-merge August 8, 2023 16:54
@lukaw3d lukaw3d merged commit 9eda808 into master Aug 8, 2023
@lukaw3d lukaw3d deleted the lw/format-intl branch August 8, 2023 16:59
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