JavaScript:使用 toLocaleString() 格式化整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21536984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript: Format whole numbers using toLocaleString()
提问by Keven
I'm using the Number.prototype.toLocaleString()
function to add commas to whole numbers. Documentation for it can be found here.
我正在使用该Number.prototype.toLocaleString()
函数为整数添加逗号。可以在此处找到它的文档。
I am writing it as follows:
我是这样写的:
Number(data).ToLocaleString('en');
In Firefox/Chrome the number is displayed like 123,456,789
. However, in IE it is displayed like 123,456,789.00
.
在 Firefox/Chrome 中,数字显示为123,456,789
。但是,在 IE 中它显示为123,456,789.00
.
1. Why is IE adding in the decimal point values?
1. 为什么IE要加上小数点数值?
2. How can I remove the decimal point values?
2. 如何去除小数点值?
Rather than creating/using a custom function, I'm really just wondering if there is an option that I can add to ToLocaleString() like en, nodecimal
. If that option is not available, I will consider a custom function.
而不是创建/使用自定义函数,我真的只是想知道是否有一个选项可以添加到 ToLocaleString() 中,例如en, nodecimal
. 如果该选项不可用,我会考虑自定义函数。
采纳答案by Guy
How about:
怎么样:
const sum = 1000;
const formatted = sum.toLocaleString("en", {
minimumFractionDigits: 0,
maximumFractionDigits: 0,
});
console.log(formatted);
for:
为了:
// 1,000
Or if you're into the money:
或者,如果您喜欢钱:
const sum = 1000;
const formatted = sum.toLocaleString("en", {
style: "currency",
currency: "USD",
minimumFractionDigits: 0,
maximumFractionDigits: 0,
});
console.log(formatted);
for:
为了:
// ,000
回答by Andy E
Which version of IE did you test in? In IE 10 and lower, toLocaleString
is based on the ECMAScript specification, which states that the function should be "implementation dependant". In IE 11, it is based on the ECMA Internationalization API, and should be consistent with Firefox 26.
你测试的是哪个版本的IE?在 IE 10 及更低版本中,toLocaleString
基于 ECMAScript 规范,该规范指出该函数应“依赖于实现”。在 IE 11 中,它基于ECMA 国际化 API,并且应该与 Firefox 26 保持一致。
To remove the decimal values in IE 10 and lower (and potentially, other older browsers), you'll have to resort to string manipulation:
要删除 IE 10 及更低版本(可能还有其他较旧的浏览器)中的十进制值,您将不得不求助于字符串操作:
Number(data).toLocaleString('en').slice(0, -3);
There's also a polyfill availablefor this API, which will work for IE 10 and lower. Including it at the moment is a little tricky, since the browser/minified build contains no actual data (because it would be huge). The data is provided separately in JSON or JSONP format, so that you can download the correct data for the user currently browsing your site.
还有一个可用于此 API的 polyfill,它适用于 IE 10 及更低版本。目前包含它有点棘手,因为浏览器/缩小版本不包含实际数据(因为它会很大)。数据以 JSON 或 JSONP 格式单独提供,以便您可以为当前浏览您网站的用户下载正确的数据。
回答by a15n
1) Refer to Andy E's (1) answer.
1) 参考 Andy E 的 (1) 回答。
2) Andy E's solution works on IE 10 and lower but seems to cause the wrong outputs on modern browsers (try it in the console with any number). Here is a safer string manipulation:
2) Andy E 的解决方案适用于 IE 10 及更低版本,但似乎在现代浏览器上导致错误输出(在控制台中尝试使用任意数字)。这是一个更安全的字符串操作:
Number(data).toLocaleString().split('.')[0];
*Andy I would have added this as a comment to your answer but I don't have enough reputation.
*安迪我会将此作为评论添加到您的答案中,但我没有足够的声誉。
回答by Dovev Hefetz
Number(data).toLocaleString().replace(/\D\d\d$/, ''); should cover any locale and browser.
Number(data).toLocaleString().replace(/\D\d\d$/, ''); 应该涵盖任何语言环境和浏览器。