Hi
Maybe this is a very old topic and has been discussed thousands of times before, but now it gives me a big headache.
In my current project, I need to receive data from a legacy program. What I expected is a double value, i.e. 3.14, and what is given to me is a float value, i.e. 3.14.
When the float value casts to a double value, to my surprise, the resulted value changed and some no-zero decimals are generated in the cast result!
In order to investigate this issue, I did a quick test and the result is very interesting.
Here is my sample code
float testFloat = (float) 3.14;
double testDouble = 3.14;
double testDouble2 = (float)3.14;
System.out.println("testFloat:" +testFloat);
System.out.println("testDouble:" +testDouble);
System.out.println("testDouble2:" +testDouble2);
Here is the result
testFloat:3.14
testDouble:3.14
testDouble2:3.140000104904175
Clearly after casting, float 3.14 changed to 3.140000104904175
Why float 3.14 does not stay as double 3.140000000000000 and how to solve it?
BTW, I an using Oracle JDeveloper 10g, results are the same whether it is running in hotpot JVM or oracle OJVM
Thanks
Howard