Possible Duplicate:
How can I create a Zerofilled value using JavaScript?
I can round to x amount of decimal places with math.round but is there a way to round left of the decimal? for example 5 becomes 05 if I specify 2 places
Possible Duplicate:
How can I create a Zerofilled value using JavaScript?
I can round to x amount of decimal places with math.round but is there a way to round left of the decimal? for example 5 becomes 05 if I specify 2 places
You're asking for zero padding? Not really rounding. You'll have to convert it to a string since numbers don't make sense with leading zeros. Something like this...
Or if you know you'd never be using more than X number of zeros this might be better. This assumes you'd never want more than 10 digits.
If you care about negative numbers you'll have to strip the "-" and readd it.
Another approach:
You could extend the
Number
object:Examples:
From https://gist.github.com/1180489
With comments:
Just for fun (I had some time to kill), a more sophisticated implementation which caches the zero-string:
If the padding count is large and the function is called often enough, it actually outperforms the other methods...