Is there a way to get the decimal separator and the thousands separator in the ECMAscript internationalization API?

I am trying to use the new Javascript internationalization API and would like to know if there is a way to get decimal and thousandth (grouping) for an Intl.NumberFormat instance?

There is a resolvedOptions method on the object, but it does not produce characters.

If someone asks a question, then for en-US it will be a comma and period . , for example, at 1,000.00 .

+6
source share
2 answers

If nothing else is like a trick solution (which does not pass the Turkish test, see the comment), you can use the output of toLocaleString() to determine the formatting information for numbers in the locale. For the current language, for example:

 var decimalSeparator = (12345.6789).toLocaleString().match(/345(.*)67/)[1]; var thousandSeparator = (12345.6789).toLocaleString().match(/12(.*)345/)[1]; var numberOfDecimals = (12345.6789).toLocaleString().match(/345(\D*)(\d+)$/)[2].length; 

The same trick can be used to format the currency using, for example, (12345.6789).toLocaleString("en-GB", { style: "currency" }) .

+1
source

I am afraid that the ECMA-402 standard does not define an API that allows you to access delimiters. Another problem - at the moment, the adoption of ECMA-402 is not as great as we wish.

So for now, if I were you, I would look at something like a CLDR JSON or iLib binding , which apparently provides this information through LocaleInfo Functions getDecimalSeparator() and getGroupingSeparator() .
BTW. iLib seems to use CLDR as a source of information.

0
source

Source: https://habr.com/ru/post/974975/


All Articles