How to use division in JavaScript
Make one of those numbers a float.
737/parseFloat(1070)
or a bit faster:
737*1.0/1070
convert to 2 decimal places
Math.round(737 * 100.0 / 1070) / 100
Also you can to use [.toPrecision(n)]
, where n
is (total) the number of digits. So (23.467543).toPrecision(4)
=> 23.47
or (1241876.2341).toPrecision(8)
=> 1241876.2
.
See MDN
Try this
let ans = 737/1070;
console.log(ans.toFixed(2));
toFixed() function will do
(737/1070).toFixed(2);
rounds the result to 2 decimals and returns it as a string. In this case the rounded result is 0.69
by the way, not 0.68
. If you need a real float rounded to 2 decimals from your division, use parseFloat((737/1070).toFixed(2))
See also