Home > Programming, Tip, Uncategorized > Javascript: zero padding numbers

Javascript: zero padding numbers

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 ]

Advertisements
  1. June 23, 2010 at 4:35 pm

    Hi Ricoch3n!

    Nice approach. too elegant!

  1. July 20, 2010 at 10:37 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: