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

Support for local decimal separator #35

Open
piotr-musialek-skyrise opened this issue Oct 5, 2021 · 4 comments · May be fixed by #36
Open

Support for local decimal separator #35

piotr-musialek-skyrise opened this issue Oct 5, 2021 · 4 comments · May be fixed by #36

Comments

@piotr-musialek-skyrise
Copy link
Contributor

How can I set what is treated as a decimal separator? I can easily add support for units in more languages, but half of the world uses comma as a decimal separator and I can't seem to find a way to switch it.

Right now:
parse('1.500 seconds') - resulting in one and a half second
parse('1,500 seconds') - resulting in one and a half thousand seconds

I need:
parse('1,500 seconds') - resulting in one and a half second

I understand that it's impossible to parse it both ways, but at least what we could have is a way to override the decimal separator based on our language we use in our apps. Maybe similar to how we add language specific units?
parse['decimalSeparator'] = ',';

@dy dy linked a pull request Oct 5, 2021 that will close this issue
@dy
Copy link
Collaborator

dy commented Oct 5, 2021

Added #36, but it conflicts with current test case '27,681 ns' === 27681ns, not 27.681ns.
Extending support to any local formats would be a breaking change.
Need additional input - which semantic for 27,681 is anticipated.

Decimal separator solution introduces dynamic regex, hoped to avoid that, but seems unavoidable.

@dy
Copy link
Collaborator

dy commented Oct 5, 2021

I wonder if manual workaround would do @piotr-musialek-skyrise?

const normDur = str => str.replace(/(\d)\,(\d+[^\d,.])/g, '$1.$2')

parseDuration(normDur('1,500 seconds')) // === 1500

@piotr-musialek-skyrise
Copy link
Contributor Author

piotr-musialek-skyrise commented Oct 5, 2021

In my opinion it is close to impossible to parse numbers without explicitly telling which decimal separator to use. My example had zeroes at the end, so it gave away a little which is correct, but think about the example like 1,505. If comma is a decimal separator this should be a little over one and a half. If period is a decimal separator it's over a thousand. Which is it? It's a 50/50 guess.

Therefore I am suggesting a switch/option or something like that. A period should be default, but it would be nice to change it.

Also the workaround you posted seems good enough for me to use it right now, so thanks for that :)

EDIT: I case anyone else would want a workaround, this regex worked for me instead:

str.replace(/(\d),(\d+[^,.])/g, '$1.$2')

@chriscalo
Copy link

As long as a locale is provided or somehow set by the caller, it seems possible to support localized number parsing.

Here is an example:

https://observablehq.com/@mbostock/localized-number-parsing

I found this from the following StackOverflow answer:

https://stackoverflow.com/a/55366435/101869

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 a pull request may close this issue.

3 participants