java.math · java.base · since Java 1.1
BigDecimal
public class BigDecimal extends Number implements Comparable<BigDecimal>Arbitrary-precision decimal arithmetic — the money type. Construct from String, divide with explicit scale and RoundingMode, compare with compareTo.
Key methods
BigDecimal(String val) / static BigDecimal valueOf(double) | Exact creation (the double CONSTRUCTOR copies binary error — avoid). |
BigDecimal add/subtract/multiply(BigDecimal) | Exact arithmetic. |
BigDecimal divide(BigDecimal, int scale, RoundingMode) | Division MUST specify rounding — 1/3 never terminates. |
BigDecimal setScale(int, RoundingMode) | Normalize decimal places (money: 2, HALF_UP or HALF_EVEN). |
int compareTo(BigDecimal) | Numeric comparison — equals() distinguishes 2.0 from 2.00! |