If you're desperate for precision use BigDecimal.
public static void main(String[] args) {
BigDecimal d = BigDecimal.valueOf(0.3d);
BigDecimal f = BigDecimal.valueOf(0.1d);
System.out.println(d.add(f));
System.out.println(d.multiply(f));
System.out.println(d);
System.out.println(f);
System.out.println(d.subtract(f));
System.out.println(d.divide(f));
System.out.println((d.subtract(f)).multiply(d.subtract(f)));
}
Output
0.4
0.03
0.3
0.1
0.2
3
0.04
Or round your result, DecimalFormat will do this quite nicely using the # symbol meaning only show decimals where necessary
double d = 0.3d;
double f = 0.1d;
DecimalFormat format = new DecimalFormat("#.##");
System.out.println(format.format(d + f));
System.out.println(format.format(d * f));
System.out.println(format.format(d));
System.out.println(format.format(f));
System.out.println(format.format(d - f));
System.out.println(format.format(d / f));
System.out.println(format.format((d - f) * (d - f)));
Output
0.4
0.03
0.3
0.1
0.2
3
0.04