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
- Why Not double for Money
- Working with BigDecimal
- Rounding and Scale
- BigInteger for Huge Numbers