简体   繁体   English

是否有任何 function 来修复 javascript 中数学计算的返回值?

[英]Is there any function to fix the returned value of a math calculation in javascript?

I'm doing this math which is (a - b) / a = number but javascript doesn't return the correct value我正在做这个数学运算,即 (a - b) / a = number 但 javascript 没有返回正确的值

Here is my code这是我的代码

6200000 - 5800000 / 6200000

it should return 0.06451 but it returns它应该返回 0.06451 但它返回

5799998.931034483

How can I get only 0.06?我怎样才能只得到 0.06?

Try using parenthesis to evaluate the subtraction first and toFixed() :尝试使用括号首先评估减法和toFixed()

 var n = ((6200000 - 5800000) / 6200000).toFixed(2); console.log(n);

use brackets使用括号

(6200000 - 5800000) / 6200000

division happens before subtraction.. to avoid it, use brackets除法发生在减法之前。为避免它,请使用括号

if you want two digits in decimal use ((6200000 - 5800000) / 6200000).toFixed(2)如果你想要两位十进制使用((6200000 - 5800000) / 6200000).toFixed(2)

((6200000 - 5800000) / 6200000).toFixed(2) ((6200000 - 5800000) / 6200000).toFixed(2)

Order of math operations.数学运算的顺序。

It'll run the divison before the subtraction.它将在减法之前运行除法。

To increase the order of the subtraction you can stick it in brackets.要增加减法的顺序,您可以将其放在括号中。

(6200000 - 5800000) / 6200000

To shrink it to 2 decimal places add a toFixed function.要将其缩小到小数点后 2 位,请添加toFixed function。

((6200000 - 5800000) / 6200000).toFixed(2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM