You should not use float unless you need to. In 99% of cases, double choice is the best choice.
int x = 1111111111; int y = 10000; float f = (float) x / y; double d = (double) x / y; System.out.println("f= "+f); System.out.println("d= "+d);
prints
f= 111111.12 d= 111111.1111
After the comment by @Matt.
float has very little accuracy (6-7 digits) and a fairly significant rounding error. double has another 9 digits of precision. The cost of using double instead of float is conditional in 99% of cases, but the cost of a subtle error due to rounding errors is much higher. For this reason, many developers recommend not using floating point at all, and highly recommend BigDecimal.
However, I believe that double can be used in most cases , provided that reasonable rounding is used .
In this case, int x has 32-bit precision, while float has 24-bit precision, even dividing by 1 may have a rounding error. double, on the other hand, has 53-bit precision, which is more than enough to get a sufficiently accurate result.
Peter Lawrey Dec 07 '10 at 15:49 2010-12-07 15:49
source share