There's .toLocaleString()
,但不幸的是,规范将其定义为“依赖于实现”——我讨厌他们这样做。因此,它在不同浏览器中的行为有所不同:
var val = 1000000;
alert(val.toLocaleString())
// -> IE: "1,000,000.00"
// -> Firefox: "1,000,000"
// -> Chrome, Opera, Safari: "1000000" (i know, it's the same as toString()!)
所以你可以看到它不可靠,因为 ECMA 团队懒得正确定义它。 Internet Explorer 在将其格式化为货币方面做得最好。你最好用自己的或其他人 http://javascript.internet.com/forms/currency-format.html的实施。
or
mine http://jsfiddle.net/R4DKn/:
(function (old) {
var dec = 0.12 .toLocaleString().charAt(1),
tho = dec === "." ? "," : ".";
if (1000 .toLocaleString() !== "1,000.00") {
Number.prototype.toLocaleString = function () {
var f = this.toFixed(2).slice(-2);
return this.toFixed(2).slice(0,-3).replace(/(?=(?!^)(?:\d{3})+(?!\d))/g, tho) + dec + f;
}
}
})(Number.prototype.toLocaleString);
仅在我自己的语言环境 (en-GB) 中的 IE、Firefox、Safari、Chrome 和 Opera 中进行了测试。