ActionScript's Number follows the IEEE 754 floating point standard
( http://en.wikipedia.org/wiki/IEEE_754 ). It's essentially lossy by
design.

It's not the really the right thing to use for financial calculations,
but ActionScript doesn't really have alternatives. It has essentially
two numeric types int (with--obviously--only integer precision) and the
above-mentioned Number.

One approach would be to do all your math in cents (or tenths of cents)
using ints and convert for display to the user. Another would be to
write your own numeric class that works to the precision you need. There
may be some out there already, since it seems like this would come up
pretty often (a quick search seems to confirm this).

-- 
Maciek Sakrejda
Truviso, Inc.
http://www.truviso.com

-----Original Message-----
From: Ken Johnson <kenjohnso...@hotmail.com>
Reply-To: flexcoders@yahoogroups.com
To: flexcoders@yahoogroups.com
Subject: [flexcoders] Rounding error
Date: Wed, 17 Dec 2008 14:15:54 -0000

Hi Everyone -

Given the following code:
var num1:Number = 0.72;
var num2:Number = 0.198;
var num3:Number;

num3 = num1;
num3 += num2;

I would expect num3 to equal 0.918.

Instead, it equals 0.9179999999999999.

And when I apply a number formatter with a precision of 2, I get 0.91.

I am performing financial calculations, and this is not acceptable.

Am I doing something wrong here?

Thanks for your help!
Ken




 


Reply via email to