Skip to main content
Inspiring
July 12, 2006
Answered

Simple Math equation in Flash BUGGSSS

I'm doing a simple Minus equation
here it is:

Question : 100.24 - 100.23
My Answer : 0.01
Flash Answer : = 0.0.00999999999999091

Question : 10.24 - 10.23
My Answer : 0.01
Flash Answer : = 0.00999999999999979

Question : 0.24 - 0.23
My Answer : 0.01
Flash Answer : = 0.01

Why when i'm over 10.00, Flash gives me a wrong answer. Also, there is somethings weird. If I put the number in textfields and I display the result, it will be different of the same equation in a trace.

I need to build a form that calculate money. It's a problem for me if decimal are not valid!

Any idea?
This topic is closed to new replies. Start a new post to keep the conversation going.
blemmoCorrect answer
Inspiring
July 12, 2006
Forgot: when you take the numbers from a textfield, they come as a string, so you have to convert them to a number first before calculating with it. The parseFloat() function does this conversion.

cheers,
blemmo

3 replies

blemmoCorrect answer
Inspiring
July 12, 2006
Forgot: when you take the numbers from a textfield, they come as a string, so you have to convert them to a number first before calculating with it. The parseFloat() function does this conversion.

cheers,
blemmo
Translate
NixyAuthor
Inspiring
July 13, 2006
That works great.
Tanx for your help!
Translate
Inspiring
July 12, 2006
That's a common problem when calculating with decimals, not only in Flash. There were some good explanations in this forum, but it seems I can't come up with the right search terms atm... In short, there are always some rounding operations when calculating with decimals, resulting in those strange results. You can try to search the forum, or read http://en.wikipedia.org/wiki/Floating_point, if you want to know more about it.
A way to avoid this is to convert the decimals into full numbers first, calculate, and then convert back to decimals. E.g., if you always have 2 decimal parts, multiply by 100 to get full numbers, do the calculation, then divide the result by 100 to move the decimal point back in place.

hth,
blemmo
Translate