# Problem with rounding floating points

• 02-17-2006
MacNilly
Problem with rounding floating points
Code:

```int convMinutes(double x) {     int mins = static_cast<int>(x) * 60;     cout << x - static_cast<int>(x);     mins = (x - static_cast<int>(x)) * 100;     cout << mins;         return mins; }```
The problem is to take a floating point number (x) and look at it as if it's a time input in format HH.MM. If I enter, say 12.59 as x, the first cout outputs .59. The second one outputs 58. .59 * 100 is not 58. I can only assume some internal rounding is messing up my conversion.

Any insights, ideas, would be much appreciated.
• 02-17-2006
iMalc
You could add half a percent to round it:
mins = (x - static_cast<int>(floor(x)) + 0.005) * 100;
• 02-17-2006
7stud
Quote:

.59 * 100 is not 58.
No, but .589999999999999999 cast to an int and THEN multiplied by 100 is 58. Everything stored on a computer is stored in binary--that's the only thing computers know how to do--and there is no exact conversion from decimal to binary for some numbers.
• 02-17-2006
swoopy
>No, but .589999999999999999 cast to an int and THEN multiplied by 100 is 58.
Actually it's more like 0.