JavaScript数学,四舍五入到小数点后两位\[重复\]

JavaScript

梅飞云

2020-04-03

我有以下JavaScript语法:

var discount = Math.round(100 - (price / listprice) * 100);

这四舍五入为整数。如何返回两位小数的结果?

第3960篇《JavaScript数学,四舍五入到小数点后两位\[重复\]》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

8个回答
前端神无 2020.04.03

这是一个工作示例

var value=200.2365455;
result=Math.round(value*100)/100    //result will be 200.24
小宇宙神奇飞云 2020.04.03

接受的答案略有不同。 toFixed(2)返回一个字符串,并且您将始终获得两个小数位。这些可能为零。如果您想取消最后的零,只需执行以下操作:

var discount = + ((price / listprice).toFixed(2));

编辑:我刚刚发现了Firefox 35.0.1中的一个bug,这意味着以上内容可能为NaN提供了一些值。
我已将代码更改为

var discount = Math.round(price / listprice * 100) / 100;

这给出了一个最多有两个小数位的数字。如果要三个,则将乘以1000,以此类推。
OP始终希望保留两位小数,但是如果toFixed()在Firefox中损坏,则需要先修复。
参见https://bugzilla.mozilla.org/show_bug.cgi?id=1134388

番长猴子 2020.04.03

我认为我看过的最好方法是将10乘以数字的幂,然后进行Math.round,最后将10除以数字的幂。这是我在 TypeScript中使用的简单函数:

function roundToXDigits(value: number, digits: number) {
    value = value * Math.pow(10, digits);
    value = Math.round(value);
    value = value / Math.pow(10, digits);
    return value;
}

还是普通的javascript:

function roundToXDigits(value, digits) {
    if(!digits){
        digits = 2;
    }
    value = value * Math.pow(10, digits);
    value = Math.round(value);
    value = value / Math.pow(10, digits);
    return value;
}
番长樱梅 2020.04.03

最快的方法 -比toFixed()更快:

两个小数

x      = .123456
result = Math.round(x * 100) / 100  // result .12

三小数

x      = .123456
result = Math.round(x * 1000) / 1000      // result .123
前端Stafan 2020.04.03

尝试使用 discount.toFixed(2);

Stafan路易 2020.04.03

如果您使用一元加号将字符串转换为MDN中记录的数字

例如:+discount.toFixed(2)

乐米亚 2020.04.03

要获得两个小数点的结果,可以这样:

var discount = Math.round((100 - (price / listprice) * 100) * 100) / 100;

将四舍五入的值乘以100以保留前两位,然后将其除以100得到实际结果。

2020.04.03

注-如果3位数精度很重要,请参阅编辑4

var discount = (price / listprice).toFixed(2);

toFixed将根据超过2个小数的值为您舍入或舍入。

示例:http//jsfiddle.net/calder12/tv9HY/

文档:https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed

编辑 -正如其他人所提到的,它将结果转换为字符串。为了避免这种情况:

var discount = +((price / listprice).toFixed(2));

编辑2-正如注释中所提到的,此函数在某种程度上会失败,例如在1.005的情况下,它将返回1.00而不是1.01。如果准确性在这个程度上很重要,那么我已经找到了这个答案:https : //stackoverflow.com/a/32605063/1726511这似乎与我尝试过的所有测试都很好。

但是,需要进行一些小的修改,上面链接的答案中的函数在四舍五入时将返回整数,因此例如99.004将返回99而不是99.00,这对于显示价格而言并不理想。

编辑3-似乎在实际收益表上固定了TOST仍在搞砸一些数字,此最终编辑似乎有效。哎呀,这么多返工!

   var discount = roundTo((price / listprice), 2);

   function roundTo(n, digits) {
     if (digits === undefined) {
       digits = 0;
     }

     var multiplicator = Math.pow(10, digits);
     n = parseFloat((n * multiplicator).toFixed(11));
     var test =(Math.round(n) / multiplicator);
     return +(test.toFixed(digits));
   }

请参阅此处的小提琴示例:https : //jsfiddle.net/calder12/3Lbhfy5s/

编辑4-你们杀了我。Edit 3对负数失败,而没有深入研究为什么在进行舍入之前将负数变为正数更容易处理,然后在返回结果之前将其变回正好。

function roundTo(n, digits) {
    var negative = false;
    if (digits === undefined) {
        digits = 0;
    }
        if( n < 0) {
        negative = true;
      n = n * -1;
    }
    var multiplicator = Math.pow(10, digits);
    n = parseFloat((n * multiplicator).toFixed(11));
    n = (Math.round(n) / multiplicator).toFixed(2);
    if( negative ) {    
        n = (n * -1).toFixed(2);
    }
    return n;
}

小提琴:https : //jsfiddle.net/3Lbhfy5s/79/

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android