When I cast 175.26 in int, it returns 175, but when I do the same with 175.0, it returns 174.
Why is it so and how to correct it? Thanks
CodePudding user response:
In your example, the diff1
value actually is 0.1749999999 . Because the IDE that you use is rounding up so you see it as 0.175, that is why you see this confusing behavior.
To check this, you could make comparison: boolean com = diff1 < 0.175;
For the exact calculation in Java, never use double or float, you could use BigDecimal instead.
CodePudding user response:
you must know how are float and double numbers represented in binary according to IEEE 754 standard reprsentation , so you can refer to any online website like ieee 754 standard, if you understand IEEE 754 standard, you will know that some float or double numbers like 0.175 doesn't have accurate representation , instead its representstion is estimated, this is the binary representation of 0.175 as float :
00111110001100110011001100110011
if you covert that number back to its corresponding float number according to IEEE 754 standard it will be :
0.17499999702
so it's just the matter of fact how float and double numbers are represented
CodePudding user response:
Refer this doc to get more understanding and 175.0 is never round to 174