如何在下面的代码中添加小数

时间:2018-07-01 13:46:35

标签: javascript html css

这是我的新手,我试图在var var行1.50上加上小数,但计算不正确,有人可以帮忙...谢谢...

if (fgh < 27) {
    total = 65;
} else if(fgh == 28) {
    total = 67;
} else {
    var val = 67 + (fgh.toFixed (0)  -  28) * 1.50;
    total = val.toFixed(0);
}

1 个答案:

答案 0 :(得分:0)

方法:toFixed(decimal_digits),将答案四舍五入为“ decimal_digits”。

例如:

  

var x = 5.95;

     

x.toFixed(0); // 6

     

x.toFixed(1); // 6.0

     

x.toFixed(2); // 5.95

     

x.toFixed(3); // 5.950

因此,您应该将最后一行替换为:

  

total = val.toFixed(1);

     

//或

     

total = val.toFixed(2);

相关问题