|
-
Nov 5th, 2006, 09:05 AM
#1
Thread Starter
Frenzied Member
Format Numbers in Javascript
hi all ,
I need a little help on a script...
I have a number from 0 to 300.
What I want to do is format them to like 030, 004, or 250.
I've tried to use string.length and stuff to put those extra zero's in front, but I can't get it to work...
Anyone?
thnx in advance
-
Nov 5th, 2006, 09:16 AM
#2
Re: Format Numbers in Javascript
I can't remember now if JavaScript used switch like PHP does, so just an IF structure here:
Code:
function padlen(numstr) {
if(numstr.length > 2) return numstr;
if(numstr.length == 2) return '0' + numstr;
if(numstr.length == 1) return '00' + numstr;
return '000';
}
And untested, of course.
-
Nov 5th, 2006, 11:29 AM
#3
Thread Starter
Frenzied Member
Re: Format Numbers in Javascript
thnx for the reply but the function only seems to return 000
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|