by kirupa | 24 December 2012
Have questions? Discuss this HTML5 / JavaScript tutorial with others on the forums.
This is a companion discussion topic for the original entry at http://www.kirupa.com/html5/rounding_numbers_in_javascript.htm
by kirupa | 24 December 2012
Have questions? Discuss this HTML5 / JavaScript tutorial with others on the forums.
It’s a nice post about rounding.
But for display you could simply use Number.prototype.toFixed.
So for example you will do:
Math.PI.toFixed(4); // '3.1416'
(8.1234).toFixed(2); // '8.12'
'$' + (2.5).toFixed(2); // '$2.50'
I mention display since it returns a string.
Nice for currency since it will add zeros.
Wow - I had no idea about that function. I’ll revise this article to mention that
:: Copyright KIRUPA 2024 //--