gpt4 book ai didi

javascript - 并非所有浏览器都支持 toLocaleString()?

转载 作者:bug小助手 更新时间:2023-10-28 10:48:09 25 4
gpt4 key购买 nike

我有这个简单的功能:

Chrome、Firefox、IE:

Number(1000000).toLocaleString()
"1 000 000" // in french system, the space is the separator instead of the comma

Opera,傲游:

Number(1000000).toLocaleString()
"1000000"

为什么 Opera 和 Maxthon 不能格式化?他们支持这种方法,但没有以正确的方式执行?

是否有任何 toLocaleString() 替换?

最佳答案

根据用户的区域设置,输出也会有所不同,即使 Number.prototype.toLocaleString他们的浏览器支持,例如在 en-GB 上对我来说,Number(1000000).toLocaleString(); 给了我 "1,000,000"

is there any toLocaleString() replacement?

为什么不写一个来做你想做的事呢?例如,

function localeString(x, sep, grp) {
var sx = (''+x).split('.'), s = '', i, j;
sep || (sep = ' '); // default seperator
grp || grp === 0 || (grp = 3); // default grouping
i = sx[0].length;
while (i > grp) {
j = i - grp;
s = sep + sx[0].slice(j, i) + s;
i = j;
}
s = sx[0].slice(0, i) + s;
sx[0] = s;
return sx.join('.');
}

现在

localeString(1000000.00001);
// "1 000 000.00001"

关于javascript - 并非所有浏览器都支持 toLocaleString()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16157762/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com