RE: Formatting Numbers using JavaScript

by David Mintz <mambomintz(at)yahoo.com>

 Date:  Fri, 24 Mar 2000 11:16:31 -0800 (PST)
 To:  hwg-languages(at)hwg.org
  todo: View Thread, Original
Well I'll be! That looks just like one I wrote with my
own hands and offered on the list some months ago. I'm
flattered.  Then someone else -- my apologies, I
forget your name -- proposed one that was more compact
and elegant. Wanna show it to us again? Every so often
we go through this -- how to format a number as
currency in Javascript. HWG oughta have a Javascript
FAQ or something.

Notice how much easier it is with Perl (-: Yeah I
know, Javascript does it on the client side and it's
mighty fast, but people can disable javascript in
their browsers whereas they can hardly disable Perl on
your server.

David Mintz
http://www.dmintzweb.com
http://www.panix.com/~dmintz

--- Peter Benoit <pbenoit(at)triton-network.com> wrote:
> This is how I do it in Javascript:
> 
> function format(num) {
> num = eval(num);
> num *= 100;
> num = Math.round(num)/100;
> 
> 	if (num - Math.floor(num) == 0) {
> 		num = num + ".00";
> 	} else {
> 		string = num.toString();
> 		parts = string.split(".");
> 		cents = parts[1];
> 			if (cents.length == 1) {
> 				num = num + "0";
> 				}
> 	}
> 
> num = "$" + num;
> return num;
> }
> 


__________________________________________________
Do You Yahoo!?
Talk to your friends online with Yahoo! Messenger.
http://im.yahoo.com

HWG: hwg-languages mailing list archives, maintained by Webmasters @ IWA