0Pricing
Java Academy · Lesson

BigDecimal for Financial Calculations

Use BigDecimal to avoid floating-point precision errors in money and tax calculations.

BigDecimal for Financial Calculations

Floating-point types (double, float) cannot represent all decimal values exactly due to binary representation. For money and taxes, use BigDecimal.

The Floating-Point Problem

Binary floating-point introduces tiny rounding errors. In financial apps these errors accumulate and cause real bugs.

double price = 0.1 + 0.2;
System.out.println(price);          // 0.30000000000000004 (WRONG!)
System.out.println(price == 0.3);   // false

// Real bug: total never equals expected
double total = 0;
for (int i = 0; i < 10; i++) total += 0.1;
System.out.println(total); // 0.9999999999999999

All lessons in this course

  1. The Math Class Essentials
  2. Integer Arithmetic & Overflow
  3. BigDecimal for Financial Calculations
  4. NumberFormat and printf
← Back to Java Academy