I'm doing this math which is (a - b) / a = number but javascript doesn't return the correct value
Here is my code
6200000 - 5800000 / 6200000
it should return 0.06451 but it returns
5799998.931034483
How can I get only 0.06?
Try using parenthesis to evaluate the subtraction first and 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)
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.
((6200000 - 5800000) / 6200000).toFixed(2)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.