Bhagwat Singh

PHP Developer Portfolio

Pad a number with leading zeroes in Javascript

As far as I know there isn’t a in-built way with Javascript to pad a number with leading zeroes to make it a fixed width like str_pad in PHP programming language. This post looks at a way to pad a number with zeroes with Javascript.

function pad(iNumber, iLength)
{
var sOutputStr= ” + iNumber;
while (sOutputStr.length < iLength) { sOutputStr = '0' + sOutputStr; } return sOutputStr; }

This entry was posted in Javascript. Bookmark the permalink.

Site Nevigation