JavaScript:使用toLocaleString()格式化整数 [英] JavaScript: Format whole numbers using toLocaleString()

查看:142
本文介绍了JavaScript:使用toLocaleString()格式化整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Number.prototype.toLocaleString()函数将逗号添加到整数。有关它的文档可以在此处找到。

I'm using the Number.prototype.toLocaleString() function to add commas to whole numbers. Documentation for it can be found here.

我写的如下:

Number(data).ToLocaleString('en');

在Firefox / Chrome中,数字显示为 123,456,789 。但是,在IE中它显示为 123,456,789.00

In Firefox/Chrome the number is displayed like 123,456,789. However, in IE it is displayed like 123,456,789.00.

1。为什么IE添加小数点值?

2。如何删除小数点值?

我真的只是想知道是否有一个选项,我不知道如何创建/使用自定义函数可以添加到ToLocaleString(),如 en,nodecimal 。如果该选项不可用,我将考虑自定义函数。

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.

推荐答案

您测试的是哪个版本的IE?在IE 10及更低版本中, toLocaleString 基于ECMAScript规范,该规范声明该函数应该是依赖于实现的。在IE 11中,它基于 ECMA国际化API ,并且应与Firefox 26保持一致。

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 10及更低版本(以及可能的其他旧浏览器)中的小数值,您将不得不求助于字符串操作:

To remove the decimal values in IE 10 and lower (and potentially, other older browsers), you'll have to resort to string manipulation:

Number(data).toLocaleString('en').slice(0, -3);

还有可用的填充,适用于IE 10及更低版本。目前包含它有点棘手,因为浏览器/缩小版本不包含实际数据(因为它会很大)。数据以JSON或JSONP格式单独提供,以便您可以为当前浏览您网站的用户下载正确的数据。

There's also a polyfill available for 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.

这篇关于JavaScript:使用toLocaleString()格式化整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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