I have this :
i=4.568;
document.write(i.toFixed(2));
output :
4.57
But i don't want to round the last number to 7 , what can i do?
JavaScript Number toFixed() The toFixed() method converts a number to a string. The toFixed() method rounds the string to a specified number of decimals.
Trunc() method. The Math. trunc() is the most popular method to remove the decimal part of JavaScript. It takes positive, negative, or float numbers as an input and returns the integer part after removing the decimal part.
Use simple math instead;
document.write(Math.floor(i * 100) / 100);
(jsFiddle)
You can stick it in your own function for reuse;
function myToFixed(i, digits) {
var pow = Math.pow(10, digits);
return Math.floor(i * pow) / pow;
}
document.write(myToFixed(i, 2));
(jsFiddle)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With