|
Re: Float Compare
|
Posted: Jul 8, 2002 7:24 PM
|
|
> Hi > > Why the below code always evaluate to be true/0? > Can anybody tell me how to do it correctly > > Float f= new Float (999999.99f); > Float f2 = new Float(1000000.00f); > > System.out.println(f.compareTo(f2)); > System.out.println(f.floatValue()== > = f2.floatValue());
Java uses IEEE format to store float and double values in memor 0.99 cannot be represented in binary in finite number of bits , so Java uses the closest value instead of exact one. And, in your case 999999.99f is closest to 1000000.00f. Therefore, both values are stored as 1000000.00f inside.
Thanks Kishori
|
|