Archive

Posts Tagged ‘zero_padding’

Javascript: zero padding numbers

April 24, 2010 2 comments

There are many ways to accomplish this in js but the following is the one I found to be the best: Let’s say you need to pad a number with leading zeros so the total digits of the number will be 5, simply do a

String('00000'+n).slice(-5);

n is the number to be padded.

[ credit goes to http://gugod.org/2007/09/padding-zero-in-javascript.html ]