# Error converting double to int

• Hi everyone !

I am facing a strange issue converting a double to int.
Here is what I do :

``````qDebug() << 0.04*double(9)/0.0016 << int(0.04*double(9)/0.0016);
``````

And the result I have is :

225 224

It gives me the same result (224) with :

``````qDebug() << int(0.04*9/0.0016);
``````

I first thought that the double value of 225 was an approximation of the real value that may be closer to 224 but when I print it, why would it print exactly 225 and not 224,3257 for example ?

Moreover, if the debugger chose to round the double value to 225, why is it rounded to 224 when converting to int then ?

When a test this computation on my calculator it gives me exactly 225...

• @HB76
All `double`s (or `float`s) in C/C++ are only approximate values. You cannot guarantee that any number, whole/integer or with fraction/decimal places has any prefect representation, other than the number `0`.

`int(doubleValue)` always rounds down. Use another method for control over rounding, or lazily `int(doubleValue + 0.5)`. Don't rely on the output of `qDebug()` to output `double`s to any particular decimal places, use something like `QString::number(double n, char format = 'g', int precision = 6)` to explicitly pick your output format.

Moreover, if the debugger chose to round the double value to 225

`qDebug()` is not "the debugger" :) Examine the value inside the actual debugger for an accurate value.

• @HB76

You are simply dealing with rounding issues on the output.

qDebug() is an output channel which is intended for debugging purposes, but it is not the debugger.

why would it print exactly 225 and not 224,3257 for example ?

How do come with this example? It has nothing to do with the actual numbers you are using.

Try

``````#include <iostream>
...
qDebug() << 0.04*double(9)/0.0016 << int(0.04*double(9)/0.0016);
cout << 0.04*double(9)/0.0016 << int(0.04*double(9)/0.0016);
cerr << 0.04*double(9)/0.0016 << int(0.04*double(9)/0.0016);
``````

It should show the same as qDebug() output

• @JonB I know that double values are approximated but normaly all 32 bits integers values should be perfectly represented by 64 bits double, and when I try :

``````double a = 0.04*double(9)/0.0016;
qDebug() << QString::number(a,'g',6);
``````

it returns me "225"...

And the most amazing fact is when I try :

``````qDebug() << QString::number(a,'g',6) << QString::number(a,'g',6).toInt();
``````

It returns "225" 225

• @koahnig Yes I just invented these number to illustrate my point

• double a = 0.04*double(9)/0.0016;

I told you to try this in the debugger if you really want to know what the `double` value is. If you had bothered to try, you would have found `224.99999999999997`. And what is `int(224.99999999999997)`, or to 6 decimal places? Try `qDebug() << QString::number(a,'g',20)`.

normaly all 32 bits integers values should be perfectly represented by 64 bits double

How does that have any relevance to the code you show? It doesn't.

• Ok I tried to run this computation on python and it gaves me :

``````a = 0.04 * 9 / 0.0016
a
``````

224.99999999999997

``````int (a)
``````

224

``````round(a)
``````

225

It was just that Qt didn't showed the real value of a even with this output format

``````QString::number(a,'g',16)
``````

`QString::number(a,'g',16)`