Friday, October 12, 2007

Zero Padding in Java Script (To add leading zeros to a number)

Some times you need to add leading zeros to numbers to form a string like '001' instead of '1'. Rather than writing separate functions or block of code to pad one, two or three zeros, I wrote a general javascript function where you can specify how many zeros to pad. I am sharing this simple javascript function here.

function zeroPad(num,count)
{
var numZeropad = num + '';
while(numZeropad.length < count) {
numZeropad = "0" + numZeropad;
}
return numZeropad;
}

The parameters are
  • num - The number to be zero padded, this can be a string or number.
  • count - The total length of the string after it is zero padded, this should be a number.
Return value will be a string.

Example:

var number1 = 5;
var count1 = 3;
var result = zeroPad(number1,count1);
alert(result);

This will return you '005'



15 comments:

Deepak said...

hi surjit i have few doubts to ask
im new to vb6.0 with access i have lakh and above record i need to update those lakh record ,i m able to do it but it takes long time can you give me another idea or solution how to do it very simple

Sujith C Jose said...

Hi Deepak,
I am not that much familiar with Microsoft Access, but I can point you to some topics that may help you. I recomment you to try the first option, since I am not sure about the 2nd one.

1) You have to optmize the database and queries for better speed. The below url gives you more info about how to effectively use Microsoft Access. Check the topic "Query Optimization in Microsoft Access" in that page.

Link 1

2) This MSDN page explains how to use Stored Queries in Microsoft Access Databases with ADOX, but you cannot have the performance and functionalities of SQL Server.(Note: I had'nt tried this) You can google search for Stored Queries for further references.

Link 2

Deepak said...

thanks for ur advice i will check with tht and let u know.
bye.........

Glen said...

Just thought I would say that I found this little script useful so thanks!

Anonymous said...

Ciao,
My question: is the following good enough for iteration?
...
var xxxzeros="00000";//insert more zeros for very long padding ;)
var count=...;//wanted number length
for (x=...)
document.write(...xxxzeros.substr(0,count-x.toString().length)...)
...

Sujith C Jose said...

hi Ciao,

This is an example for iteration.

function test()
{
var xxxzeros="00000"; //Put as many zeros
var count=xxxzeros.length;
var x =0;
for (x=count;x>0;x--)
{
document.write(xxxzeros.substr(0,x));
document.write("<br>");
}
}

Calling this function will give you the result

00000
0000
000
00
0

Anonymous said...

Ciao again, you did it soo easily...Big THX! :)

Anonymous said...

Thanks for this Sujith.

Helped me out a lot!

cheers,

Chris

goodenov said...

I use:

function padZeros(Num, Zs)
{
return(("00000000000000000000"+Num).slice(-Zs));
}

This can pad op to 20 zeros, but can easily be changed.

Sujith C Jose said...

Hi goodenov,

Yes, this is a nice function, thanks for sharing it. The only advantage of my function is that the count can be any number.

installero said...

Thanx

rob said...

@goodenov:
I like your function, only thing is when the padding is smaller than the number it cuts if off. zero(12345,3) returns 123 instead of 12345.
A fix for that could be:
var zero = function (a,b) {
return (a.toString().length>b)?a:('00000000000000000000'+a).slice(-b);
}

Seth Wegner said...

Thanks! I googled and found this, exactly what I was looking for.

Dave said...

Thanks!

tominko said...

ii=5
---------
ii=("000"+ii).match(/(\d{3})$/)[1];

---------
result:
005