How to round off double 3.09923 to exactly 3.1000 ?

I have read a lot of posts about this and so far, none of them is helpful.. So without further ado, I seek help from here.

Thanks

No, it is not. 3.1000 is not a number that a floating point can handle. Remember, floats are never exact.

You can make 3.09923 print out as 3.10, but if you try to print to 4 places you'll get 3.0992.

Try this website: IEEE-754 Floating Point Converter

What do you need to do with the 3.1000?

int valInTenths = 10*(3.09923)+0.5;

valInTenths will end up with 31. 31/10 is exactly 3.1 (but you have to keep track of the fact that it's in tenths separately)

Remember, floats are never exact.

Except for the ones that are.
Like 0.5, 0.25, etc.

AWOL:
Except for the ones that are.
Like 0.5, 0.25, etc.

I think any fraction equal to n / 2^j can be exactly represented in binary. The denominator must be a power of 2.

Likewise, in decimal the denominator must be a number whose only factors are 2 and/or 5 for the fraction to be exactly represented.

AWOL:
Except for the ones that are.
Like 0.5, 0.25, etc.

Or 2.0004885196685791015625.
(2 + 2-11 + 2-22)

oqibidipo:
Or 2.0004885196685791015625.
(2 + 2-11 + 2-22)

because it is
(222 + 211 + 2)/2-21 == 4196354/2-21?

AWOL:
Except for the ones that are.
Like 0.5, 0.25, etc.

OK, well of course there are some that are. But you never count on it unless you've done the maths.

aarg:
because it is
(222 + 211 + 2)/2-21 == 4196354/2-21?

No, that would be a very big number - you mean 4186354 x 2-21

2 + 2-11 + 2-22 = 2 + 1/211 + 1/222 = 2 + 211/222 + 1/222 = 2 + (211 + 1)/ 222

So, the denominator of the fractional part is a power of 2. Hence, the fraction can be exactly represented in a finite number of bits to the right of the binary point.