有没有办法在ECMAscript Internationalization API中获取小数和千位分隔符? [英] Is there a way to get the decimal and thousands separator in ECMAscript Internationalization API?

查看:97
本文介绍了有没有办法在ECMAscript Internationalization API中获取小数和千位分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用新的 Javascript国际化API ,以及想知道是否有办法获得 Intl.NumberFormat 实例的小数和千位(分组)分隔符?

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?

resolvedOptions对象的 方法,但不提供符号。

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

如果有人想知道,那么对于en-US,这些是逗号和句点,例如 1,000.00

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

推荐答案

如果没有别的,作为一种技巧解决方案(传递土耳其测试;见评论),您可以使用 的toLocaleString() 确定有关区域设置中的数字格式的信息。对于当前的语言环境,例如:

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;

同样的技巧可用于货币格式化,例如使用(12345.6789).toLocaleString(en-GB,{style:currency})

The same trick can be used for currency formatting, using e.g. (12345.6789).toLocaleString("en-GB", { style: "currency" }).

这篇关于有没有办法在ECMAscript Internationalization API中获取小数和千位分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆