7 Replies Latest reply on Jun 13, 2011 2:26 AM by Xtreame96

# Math.round produces inconsistent results

We've been developing a tax calculator using the Flash 4 SDK in Flash Builder 4, and have experienced difficulties with rounding numbers - for example, the following statement:

Alert.show(Math.round((8.325 * 100.0)) + ":" + Math.round(832.5));

Both of the values rounded above should equal 833, however, the first one shows 832, while the second shows 833.  If both of the expressions inside the Math.round() functions equal 832.5, then how are the resulting values different?

• ###### 1. Re: Math.round produces inconsistent results

May be in the first case, the Math.round would perform its round off operation first and then multiplies.

• ###### 2. Re: Math.round produces inconsistent results

So how can we achieve accurate rounding results?

The following statement shows 8.32, as opposed to 8.33:

We're confident that there's a way a produce accurate results in Flex, however, we have yet to find it.

• ###### 3. Re: Math.round produces inconsistent results

Math.round is working correctly, if you trace(8.325 * 100) you'll see 832.4999999999999 because of floating point multiplication.

This post might be helpful: http://joshblog.net/2007/01/30/flash-floating-point-number-errors/

• ###### 4. Re: Math.round produces inconsistent results

Thanks, Shongrunden - I found that same link literally a minute or two before I saw your response - after some preliminary testing, it looks like the following functions work well as a workaround:

`/** * Corrects errors caused by floating point math. */public function correctFloatingPointError(number:Number, precision:int = 5):Number{     //default returns (10000 * number) / 10000     //should correct very small floating point errors     var correction:Number = Math.pow(10, precision);     return Math.round(correction * number) / correction;}/** * Tests if two numbers are almost equal. */public function fuzzyEquals(number1:Number, number2:Number, precision:int = 5):Boolean{     var difference:Number = number1 - number2;     var range:Number = Math.pow(10, -precision);     //default check:     //0.00001 < difference > -0.00001     return difference < range && difference > -range;}`
• ###### 5. Re: Math.round produces inconsistent results

If I was doing a money calculator, I'm not sure I would use Math.round().  I

think I would use toPrecision or toFixed to display the current values.  I

believe most calculators keep hidden digits.

• ###### 6. Re: Math.round produces inconsistent results

By using the CorrectFloatingPoint error function above with the following Round function, the rounding is consistent:

`public function Round(numberVal:Number, roundTo:Number):Number{     return CorrectFloatingPointError(Math.round(CorrectFloatingPointError(numberVal *                CorrectFloatingPointError(Math.pow(10, roundTo)))) /                CorrectFloatingPointError(Math.pow(10, roundTo)));}`

However, any value passed to the Round function should also be corrected - for example:

var x:Number = Round(CorrectFloatingPoint(90.00 * .0925));

• ###### 7. Re: Math.round produces inconsistent results

The answer to all your troubles and some very elegant solutions can be found here: