[thelist] javascript question on formatting numbers.

Jeff C jeffc33 at hotmail.com
Fri Dec 21 13:53:42 CST 2001


>From: "Richard Bennett" <richard.bennett at skynet.be>
>Date: Thu, 20 Dec 2001 23:30:44 +0100
>
>Hi,
>I've done something like that for displaying timers in Javascript.
>What I wanted was 0-9 to have a leading 0, so all numbers had two digits:
>
>pad = function(int) {
>       return int=(int<10)?"0"+int:int
>}
>
>So,
>
>status=pad(3)
>
>will show 03
>
>You can do this with more zeros, but you'll want to use a switch to cater
>for the different values.
>
>Don't forget that these are no longer integers, but strings now , so if you
>want to do any calculations with them you should parseInt() them.


Be careful using parseInt() here.  If the string starts with a 0 it
will be parsed as an octal number.

jeffc

_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp.





More information about the thelist mailing list