Accessibility Navigation

Pad A Javascript Number

Posted on 26th March 2009 by Jacob

When doing a lot of front end scripting with Javascript you often find a lot of useful functions missing and so you create your own.

To keep design and content consistent I often pad numbers with zeros to ensure they are all the same width. There is no built in function to do this, so I wrote my own:

function pad(numNumber, numLength){
	var strString = '' + numNumber;
	while(strString.length<numLength){
		strString = '0' + strString;
	}
	return strString;
}

Enjoy!


Have Your Say

Have Your Say Form









Comments

  • Be the first to add your voice to this post!

    Start the discussion and leave your views or thoughts by writing a comment using the form above.