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

Allow formatting of number strings #1492

Open
sohrab- opened this issue Oct 30, 2024 · 4 comments
Open

Allow formatting of number strings #1492

sohrab- opened this issue Oct 30, 2024 · 4 comments

Comments

@sohrab-
Copy link

sohrab- commented Oct 30, 2024

Is your feature request related to a problem? Please describe.

The number formatter in this package allows number or bigint input only.

When working with very larger numbers, we typically use other libraries that can deal with high precision. However, in order to use next-intl number formatter, we have to turn those numbers back into native number values, which would be lossy and produce incorrect values.

Describe the solution you'd like

Intl.NumberFormat already supports a string param that it can then format losslessly in modern browsers. Seeing how we delegate to this library anyway, it would be good to broaden the type of number() function.

This way we can pass large numbers as string to the formatter and get a correct representation out.

Describe alternatives you've considered

My workaround is to just get the locale and do my own Intl.NumberFormat formatter. But then I have to forego or re-implement all the nice locale-awareness and caching that next-intl has already got.

@sohrab- sohrab- added enhancement New feature or request unconfirmed Needs triage. labels Oct 30, 2024
@amannn
Copy link
Owner

amannn commented Oct 31, 2024

Interesting, could definitely be worth considering—thanks for the report!

This will need some investigation first, e.g.:

  1. Check if intl-messageformat supports strings for numbers in messages. We had a similar story with allowing date strings in format.dateTime(…) (see feat: Support ISO 8601 date strings with full precision for all formatting functions where dates can be passed #758 (comment)), which unfortunately had to be delayed for the time being.
  2. Verify browser and server runtimes support for string params.

@sohrab-
Copy link
Author

sohrab- commented Nov 14, 2024

Browser and runtime support seem decent (except Deno):

image

intl-messageformat, or rather intl-numberformat does seem to only support number atm. I asked the question over there as well: formatjs/formatjs#4699

@amannn
Copy link
Owner

amannn commented Nov 20, 2024

One implication of this in combination with #1499 would be that we can currently do this (ref):

"{count} followers"
// ^? {count: string}

"{count, number} followers"
// ^? {count: number}

That helps to make sure you're using the number formatter when passing a number.

If we allow numbers as strings, we'd allow number | string for the number formatter. Note that we can still keep the validation for not allowing to pass a number to simple params.

@amannn
Copy link
Owner

amannn commented Nov 20, 2024

A note from https://x.com/SlexAxton/status/1859242191408746751 on support of string-based numbers in Format.JS:

it does support them i think, just the types are wrong

Might be worth checking if this is something we could safely use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants