[thelist] javascript question on formatting numbers.

Richard Bennett richard.bennett at skynet.be
Thu Dec 20 16:32:22 CST 2001


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.

Cheers,
Richard.

----- Original Message -----
From: "Alliax" <damiencola at wanadoo.fr>
To: <thelist at lists.evolt.org>
Sent: Thursday, December 20, 2001 11:15 PM
Subject: [thelist] javascript question on formatting numbers.


> Hello,
>
> I would like to know if it is possible to format an integer to look like
this:
> 00012 for integer 12
>
> I know I could do
> var = "000" + 12
> and var would be "00012"
>
> but I'd like to know if I can format the integer to be say 5 digits long,
> whatever value it is, ie. "00421" for 421
>
> thank you
>
> Cordialement,
>
> __ Alliax         ~CV : http://LingoParadise.com/cv.php
> Un site pour Toulon : http://ToulonParadise.com
> Un site pour Renaud : http://rfaucilhon.multimania.com
> Un site pour Director : http://LingoParadise.com
>
>
>
> --
> For unsubscribe and other options, including
> the Tip Harvester and archive of TheList go to:
> http://lists.evolt.org Workers of the Web, evolt !
>





More information about the thelist mailing list