on-internet
← Community

Rounding a float number [duplicate]

Stack Overflow ·

I want to calculate 0.7% of an integer n, and then round the result to the nearest integer. Here is what happens with n = 656500: round(656500 * 0.7 /100) provides a wrong result because 656500 * 0.7 / 100 provides 4595.499999999999 and then round(4595.499999999999) = 4595. I was expecting 4596. How to cope with that? I know float numbers are not always well represented internally.

Comments

Sign in or sign up to comment and upvote.

No comments yet.