0Pricing
Java Academy · Lesson

Working with BigDecimal

Exact decimal arithmetic.

Exact Decimal Math

BigDecimal from the java.math package represents decimal numbers with arbitrary precision and no hidden rounding errors. It is the standard tool for financial calculations.

Creating a BigDecimal

The safest way to make a BigDecimal is from a string. This preserves the exact digits you typed.

import java.math.BigDecimal;

public class Main {
    public static void main(String[] args) {
        BigDecimal amount = new BigDecimal("42.50");
        System.out.println(amount);
    }
}

All lessons in this course

  1. Why Not double for Money
  2. Working with BigDecimal
  3. Rounding and Scale
  4. BigInteger for Huge Numbers
← Back to Java Academy