-
Notifications
You must be signed in to change notification settings - Fork 36
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
Comments
Added #36, but it conflicts with current test case Decimal separator solution introduces dynamic regex, hoped to avoid that, but seems unavoidable. |
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 |
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 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:
|
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: |
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 secondparse('1,500 seconds')
- resulting in one and a half thousand secondsI need:
parse('1,500 seconds')
- resulting in one and a half secondI 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'] = ',';
The text was updated successfully, but these errors were encountered: