![](/img/trans.png)
[英]JavaScript function is not returning any boolean value or ticket calculation is wrong
[英]Is there any function to fix the returned value of a math calculation in javascript?
我正在做这个数学运算,即 (a - b) / a = number 但 javascript 没有返回正确的值
这是我的代码
6200000 - 5800000 / 6200000
它应该返回 0.06451 但它返回
5799998.931034483
我怎样才能只得到 0.06?
尝试使用括号首先评估减法和toFixed()
:
var n = ((6200000 - 5800000) / 6200000).toFixed(2); console.log(n);
使用括号
(6200000 - 5800000) / 6200000
除法发生在减法之前。为避免它,请使用括号
如果你想要两位十进制使用((6200000 - 5800000) / 6200000).toFixed(2)
((6200000 - 5800000) / 6200000).toFixed(2)
数学运算的顺序。
它将在减法之前运行除法。
要增加减法的顺序,您可以将其放在括号中。
(6200000 - 5800000) / 6200000
要将其缩小到小数点后 2 位,请添加toFixed
function。
((6200000 - 5800000) / 6200000).toFixed(2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.