> -----Original Message-----
> From: Ray Champagne [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, June 08, 2005 4:35 PM
> To: CF-Community
> Subject: stupid Javascript question
> 
> I am trimming this question down to the nitty gritty for simplicity's
> sake:
> 
> Why does this javascript:
> 
> var price = 3 * 12.95;
> 
> give me this result:
> 
> 38.849999999999994

This is a side-effect of floating-point vrs integer math.

According to the standards organizations this is, in fact, the correct
answer for a proper JavaScript implementation which uses "double precision
floating point" math.

In JavaScript v3 and up you can use the "toFixed()" and "toPrecision()"
methods of the "Number" object to address this.

In your case I think (untested) that this will give you what you need:

var price = 3 * 12.95;
price.toFixed(2);

In this case "2" is the number of decimal places you want - the value will
be rounded if the raw math returns more than two.  You _might_ be able to do
this, but I'm not sure:

var price = (3 * 12.95).toFixed(2);

I don't think that would work... but it might.

Jim Davis






~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Discover CFTicket - The leading ColdFusion Help Desk and Trouble 
Ticket application

http://www.houseoffusion.com/banners/view.cfm?bannerid=48

Message: http://www.houseoffusion.com/lists.cfm/link=i:5:160016
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/5
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:5
Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.5
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54

Reply via email to