我需要使用 JavaScript 将小数四舍五入到六位,但我需要考虑旧版浏览器,所以我 can't rely on Number.toFixed
The big catch with toExponential, toFixed, and toPrecision is that they are fairly modern constructs not supported in Mozilla until Firefox version 1.5 (although IE supported the methods since version 5.5). While it's mostly safe to use these methods, older browsers WILL break so if you are writing a public program it's recommended you provide your own prototypes to provide functionality for these methods for older browser.
我正在考虑使用类似的东西
Math.round(N*1000000)/1000000
向旧版浏览器提供原型(prototype)的最佳方法是什么?
最佳答案
试试这个:
if (!Number.prototype.toFixed)
Number.prototype.toFixed = function(precision) {
var power = Math.pow(10, precision || 0);
return String(Math.round(this * power)/power);
}
关于javascript - 如何在 JavaScript 中为 Number.toFixed 编写原型(prototype)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/337112/