Working with Money

Figure 457. Limited float precision Slide presentation Create comment in forum
final float result = 0.99f - 0.1f -0.1f -0.1f;
System.out.println(result);
0.68999994

Figure 458. Limited double precision Slide presentation Create comment in forum
final double result = 0.99 - 0.1 -0.1 -0.1;
System.out.println(result);
0.6900000000000001

Figure 459. Using BigDecimal Slide presentation Create comment in forum
final BigDecimal zero_dot_99 = new BigDecimal("0.99");
final BigDecimal zero_dot_1 = new BigDecimal("0.1");

BigDecimal result = zero_dot_99.
  subtract(zero_dot_1).
  subtract(zero_dot_1).
  subtract(zero_dot_1);

System.out.println(result);
0.69

Figure 460. BigDecimal features Slide presentation Create comment in forum
  • Higher memory allocation hosting higher precision.

  • Immutable instances

  • Calculation performance penalty.

  • Clumsy interface.