Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to use the new Javascript internationalization API, and would like to know if there is a way to get the decimal and thousands (grouping) separator for a Intl.NumberFormat instance?

There is a resolvedOptions method on the object, but that does not provide the symbols.

In case anybody's wondering, then for en-US, these would be a comma , and period ., such as in 1,000.00.

like image 431
ragulka Avatar asked Sep 08 '14 11:09

ragulka


People also ask

What is Internationalization API?

Internationalization is the process of enhancing an application to support multiple languages across various regions. Internationalization is abbreviated as i18n. The kony. i18n namespace provides a comprehensive set of functions for developing multilingual applications.

How do you separate thousands?

The decimal separator is also called the radix character. Likewise, while the U.K. and U.S. use a comma to separate groups of thousands, many other countries use a period instead, and some countries separate thousands groups with a thin space.

How do you print a number with commas as thousands separators in JavaScript?

To comma-separate thousands in a big number in JavaScript, use the built-in toLocaleString() method. It localizes the number to follow a country-specific number formatting. To separate thousands with commas, localize the number to the USA.


1 Answers

If nothing else, as a trick solution (that doesn't pass the Turkey Test; see comment), you can use the output of toLocaleString() to determine information about number formatting in a locale. For the current locale, 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 for currency formatting, using e.g. (12345.6789).toLocaleString("en-GB", { style: "currency" }).

like image 169
bzlm Avatar answered Nov 15 '22 00:11

bzlm