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

痞子三分冷 提交于 2019-12-07 06:04:47

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/25722876/is-there-a-way-to-get-the-decimal-and-thousands-separator-in-ecmascript-internat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!