Below is the syntax highlighted version of Financial.java
from §9 Scientific Computation.
/************************************************************************* * Compilation: javac Financial.java * Execution: java Financial * * *************************************************************************/ import java.math.BigDecimal; import java.math.RoundingMode; public class Financial { public static void main(String[] args) { // binary floating point arithmetic double a = 1.09 * 50; System.out.println("1.09 * 50 = " + new BigDecimal(a)); System.out.println("rounds to " + Math.round(a)); double b = 1.14 * 75; System.out.println("1.14 * 75 = " + new BigDecimal(b)); System.out.println("rounds to " + Math.round(b)); double c = 1.05 * 0.70; System.out.println("1.05 * 0.70 = " + new BigDecimal(c)); System.out.println("rounds to " + Math.round(c)); // exact arithmetic BigDecimal a1 = new BigDecimal("1.09"); BigDecimal a2 = new BigDecimal("50"); BigDecimal a3 = a1.multiply(a2); System.out.println(a3 + " " + a3.setScale(0, RoundingMode.HALF_EVEN)); BigDecimal b1 = new BigDecimal("1.14"); BigDecimal b2 = new BigDecimal("75"); BigDecimal b3 = b1.multiply(b2); System.out.println(b3 + " " + b3.setScale(0, RoundingMode.HALF_EVEN)); } }