I want to have the right 6999 but the code prints 6998, is there any way to implement it in C/C++?

#include <iostream> using namespace std; int main() { double x = 69.99; int xi = x*100; cout << xi << endl; return 0; }

## Answer

Your compiler is probably using the IEEE 754 double precision floating point format for representing the C++ data type `double`

. This format cannot represent the number `69.99`

exactly. It is stored as `69.989999999999994884`

. When you multiply this value with `100`

, the result is slightly smaller than `6999`

.

When implicitly converting floating-point numbers to integers, the number is always rounded towards zero (positive numbers get rounded down, negative ones get rounded up).

If you don’t want to always round the result towards zero, you can change the line

`int xi = x*100;`

to

`long xi = lround( x*100 );`

which will not always round the number towards zero, but will instead always round it to the nearest integer.

Note that you must `#include <cmath>`

to be able to use `std::lround`

.