I may be missing something from your explanation. Are you seeing erroneous
calculations? Are there bugs? What exactly is the problem we are trying to
solve.

AFAIK, 2 digit accuracy is a given. In fact, it goes many digits higher than
that (I forgete exactly how many -- too long since my University days).

But that's what I meant by "Number has a very high precision". If you need
two-digit accuracy, just round all Number to two decimal places and it'll be
correct.

x = Math.round(x*100.0) / 100.0; //round x to 2 decimal places

More generally,

x = Math.round(x*10^n) / 10^n; //round to n decimal places

Or,

Math.floor(x*100.0) / 100.0; //ensures you never round up

http://www.google.co.nz/search?q=round+float+decimal+places has a whole
bunch of people wanting 2 digit accuracy.

If you're still concerned, someone else mentioned using integers to store
cents. That's usually my approach, but I've worked on codebases that do the
above and not had any problems.

Server and client-side implementations will be the same. AS3 floats are IEEE
compliant.

Datetimes between server and client however ... watch out :-)

On Thu, Dec 18, 2008 at 06:25, Paul Andrews <[email protected]> wrote:

>    Jules,
>
> For financial applications, it's far more problematic than testing for
> equality.
>
> For example, if you are applying a discount to a price the resulting price
> must have exact two digit decimal accuracy, otherwise by the time you have
> discounted a few items you have an invoice whose total doesn't match the sum
> of the discounted items as shown on the invoice. That tends to upset
> customers.
>
> The other thing to consider is that such arithmetic must be consistent on
> server as well as client, otherwise you can end up with server-side reports
> that don't match client-side displays.
>
> Paul
>
> ----- Original Message -----
> *From:* Jules Suggate <[email protected]>
> *To:* [email protected]
> *Sent:* Wednesday, December 17, 2008 4:36 PM
> *Subject:* Re: [flexcoders] Rounding error
>
> Valdhor already gave you all the info you need, but I like the sound of my
> own fingers typing. I also like to procrastinate, so I'm in heaven right now
> :p
>
>    - Some numbers that are easy to represent in decimal are hard to
>    represent in binary, just as some numbers that are easy to represent in 
> base
>    60 are hard to represent in decimal (such as 1/3).
>    - Your calculations are safe enough: Number has a very high precision
>    and calculations are carried out with full precision. It's just when 
> testing
>    for equality that you can run into problems -- I'm guessing this is where
>    you noticed the discrepancy?
>    - When comparing for floating-point numbers for equality, you need to
>    specify a tolerance too. Most unit-testing frameworks provide this for 
> their
>    floating-point asserts. And when I was doing financial apps, we did the 
> same
>    thing. If you *really* need higher precision, you'll need a BigNumber
>    implementation for AS3, of which there are several. The AS3Crypto library
>    has one I believe...
>
> Chyaaaars :)
> Jules
>
> On Thu, Dec 18, 2008 at 03:15, Ken Johnson <[email protected]>wrote:
>
>>   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
>>
>>
>
>
> --
> Cheers,
> Jules
> --
> Jules Suggate
> Owner and Technical Lead
> Uphill Sprint Limited
>
> +64-21-157-8562
>
>  
>



-- 
Cheers,
Jules
--
Jules Suggate
Owner and Technical Lead
Uphill Sprint Limited

+64-21-157-8562

Reply via email to