Arafat
Arafat

Reputation: 74

Math.round() wrong calculation in as3?

Can anyone explain this?

enter image description here

what am I doing wrong?

Upvotes: 0

Views: 7734

Answers (2)

Diego
Diego

Reputation: 1569

Math.Round(x:Number) rounds x to the nearest integer value. In your case 28 is the nearest integer value for 28.499999999999996. So here the behavior is correct. What is weird is that 0.285 * 100 is not 28.5, but that is a consequence of the precision of the Number class in as3. Here is a little more information about this and a possible solution:

Innacurate math results

Also you can see this SO question:

Very strange number operation issue

Hope this helps.

Upvotes: 3

Devon_C_Miller
Devon_C_Miller

Reputation: 16518

Round is doing the correct thing. 0.285 cannot be exactly represented as a binary floating point value. As you see, when multiplied by 100 it approximates to 28.4999999... which is less than 28.5, so the value is rounded down.

Upvotes: 4

Related Questions