我正在做这样的数学运算,这是(a-b)/ a =数字,但是javascript无法返回正确的值
这是我的代码
6200000 - 5800000 / 6200000
它应该返回0.06451但它返回
5799998.931034483
我怎么只能得到0.06?
我正在做这样的数学运算,这是(a-b)/ a =数字,但是javascript无法返回正确的值
这是我的代码
6200000 - 5800000 / 6200000
它应该返回0.06451但它返回
5799998.931034483
我怎么只能得到0.06?
Try using parenthesis and
toFixed()
:You should surround the subtract operation with parentheses and then call the function
Number.prototype.toFixed
使用括号
(6200000 - 5800000) / 6200000
在减法之前进行除法..为避免这种情况,请使用方括号
if you want two digits in decimal use
((6200000 - 5800000) / 6200000).toFixed(2)
((6200000-5800000)/ 6200000).toFixed(2)
数学运算的顺序。
减法之前将进行除法运算。
要增加减法的顺序,可以将其放在方括号中。
(6200000 - 5800000) / 6200000
To shrink it to 2 decimal places add a
toFixed
function.((6200000 - 5800000) / 6200000).toFixed(2)