javascript - 实现 toFixed() 的最可靠方法是什么?

标签 javascript

我正在尝试以可靠的方式实现 toFixed() 以舍入到小数位(当前的 toFixed() 函数在不同的浏览器中返回不同的结果)。

我的想法是用 Number.prototype.toFixed = function(c){}; 实现它

我尝试了很多选项,但似乎只有一个效果不错,但我对此没有信心:

通过多次乘/除 10 并四舍五入((0.069).toFixed(2); 返回“0.06999999999999999”):

Number.prototype.toFixed = function(c){
    var c =  isNaN(c = Math.abs(c)) ? 0 : c;
    var n = this;
    for(var i=0; i<c; i++){
        n *= 10;
    }
    n = Math.round(n);
    for(var i=0; i<c; i++){
        n /= 10;
    }
    n = (n+"").split(".");
    if(c==0){
        return n[0];
    }
    if(n[1] == void 0){
        n[1] = "";
    }
    while(n[1].length<c){
        n[1]+="0";
    }
    return n[0]+"."+n[1];
};

通过将数字作为字符串进行管理(我仍然对此有错误,例如:(0.0999).toFixed(2) 给我“1.10”)

Number.prototype.toFixed = function(c){
    var c =  isNaN(c = Math.abs(c)) ? 0 : c;
        var d = (this+"").split(".");
        if(d[1] == void 0){
            d[1] = "";
        }
        if(d[1].length>c){
            if(parseInt(d[1].charAt(c))>=5){
                var cont = 0;
                while(cont<c-1&&d[1].charAt(cont)==='0'){
                    cont++;
                }
                var temp="";
                while(cont--){
                    temp += "0";
                }
                d[1]=temp+(parseInt(d[1].substring(0,c))+1)+"";
                if(d[1].length>c){
                    d[0]=(parseInt(d[0])+1)+"";
                    d[1]=d[1].substring(1);
                }
            } else {
                d[1] = d[1].substring(0,c);
            }
        }
        if(c==0){
            return d[0];
        }
        while(d[1].length<c){
            d[1]+="0";
        }
        return d[0]+"."+d[1];
};

乘以/除以 10^c 并四舍五入我没有发现任何问题,但我不太自信:

Number.prototype.toFixed = function(c){
    var c =  isNaN(c = Math.abs(c)) ? 0 : c;
    var n = this;
    var z = "1";
    for(var i=0; i<c; i++){
        z+="0";
    }
    n = Math.round(n*z);
    n /= z;
    n = (n+"").split(".");
    if(c==0){
        return n[0];
    }
    if(n[1] == void 0){
        n[1] = "";
    }
    while(n[1].length<c){
        n[1]+="0";
    }
    return n[0]+"."+n[1];
};

我最好的选择是字符串操作,因为您不会混淆 float 的不确定性,尽管它比我想象的更难调试,而且我开始相信我永远不会完美。除了这些,还有其他人已经实现了吗?

最佳答案

这是我使用的代码:

function RoundNumber(input, numberDecimals)
{
  numberDecimals = +numberDecimals || 0; // +var magic!

  var multiplyer = Math.pow(10.0, numberDecimals);

  return Math.round(input * multiplyer) / multiplyer;
}

例子:

console.log( RoundNumber(1234.6789, 0) ); // prints 1234
console.log( RoundNumber(1234.6789, 1) ); // prints 1234.6
console.log( RoundNumber(1234.6789, 2) ); // prints 1234.67
console.log( RoundNumber(1234.6789, -1) ); // prints 1230

我在 Chrome 和 Firefox 中使用过它。

关于javascript - 实现 toFixed() 的最可靠方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19014494/

相关文章:

javascript - 没有 src 的 iframe 但仍然有内容?

javascript - span 标签的内容被 chop

javascript - 如何将附加文本附加到 UIWebView

javascript - 更改 CSS 内部背景颜色

javascript - 如何构建我的 javascript/jquery 代码?

javascript - AngularJS - 从 ng-repeat 获取当前对象

javascript - 将 reducer 合并为另一个 reducer 中的键

php - 用于表单验证的 jQuery post 函数只允许 alert() 显示返回的数据

javascript - 如何从 Phonegap 创建 Android 通知?

javascript - 关于 MDN 文档中关闭示例的适当性