I would like to format my numbers to always display 2 decimal places. lets say: I have a number => 21.268998
, the o/p I'm looking is to chop the rest of the decimal point and keep only the first 2 i.e:
21.26
however with the tofixed
or toPrecision
approach to always rounds to a certain decimal which is causing issues when a number is 99.999999, it rounds to 100.000 which is not right.
var num1 = "1";
document.getElementById('num1').innerHTML = (Math.round(num1 * 100) / 100).toFixed(2); // this is showing correctly
var num2 = "99.99999";
document.getElementById('num2').innerHTML = (Math.round(num2 * 100) / 100).toFixed(2);// this is incorrect=> I want to show 99.99
any idea how to get the first numbers to show always without rounding them off to the next number.
Jsfiidle: