double radius = 5;
double area = Math.PI * Math.pow(radius, 2);
// System.out.println(area);
BigDecimal bd = new BigDecimal(area).setScale(2, HALF_UP);
// BigDecimal bd = new BigDecimal(area).setScale(2, ROUND_HALF_UP);
System.out.println(bd.toString());
The above output gets printed as 78.54, but if I perform the same operation via the calculator (windows calc interface) the output comes out as 78.57 (i.e. 22/7 * 25).
Why is there an inconsistency
Are you using
22/7as an approximate value for PI? Because22/7is3.142857142857...where PI is approximately3.14159.... This explains your rounding inconsistencies.The approximation of PI that is used in the JVM is documented here. According to the JavaDoc, it is: