Is there a way to get the decimal and thousands se

2019-05-01 06:42发布

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.

2条回答
甜甜的少女心
2楼-- · 2019-05-01 06:57

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" }).

查看更多
相关推荐>>
3楼-- · 2019-05-01 07:17

I'm afraid ECMA-402 standard does not define the API that let you access separators. There is also another problem - at the moment the adoption of ECMA-402 is not as wide as we wish.

Therefore for the time being, if I were you I would look to something like CLDR JSON bindings or iLib which apparently provides these information through LocaleInfo's getDecimalSeparator() and getGroupingSeparator() functions.
BTW. iLib seems to use CLDR as a source of information.

查看更多
登录 后发表回答