Java BigDecimal Class in the java.math Package

Introduces how to add, subtract, multiply, divide, and round with Java BigDecimal

BigDecimal Overview

When calculating floating-point values in Java, you should use the BigDecimal class.

Using the float or double type may cause errors.

When performing strict money calculations, especially when dealing with foreign currencies, the BigDecimal class is essential.
Because the calculation method is different from using ordinary variables, it may feel difficult until you get used to it. However, it is necessary for precise calculations, so it is worth learning.

Here, we will introduce how to add, subtract, multiply, divide, and round with Java BigDecimal.

What Is BigDecimal?

BigDecimal is a class that can represent and calculate decimal numbers of arbitrary length.

It represents decimal numbers with a combination of an integer part and the number of digits after the decimal point, called scale.

BigDecimal Arithmetic Operations

BigDecimal provides calculation methods corresponding to the four arithmetic operations.

Method Arithmetic operation
add() Addition
subtract() Subtraction
multiply() Multiplication
divide() Division

Let’s look at an example.

package com.devkuma.basic.bigdecimal;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class BigDecimalArithmetic {
    public static void main(String[] args) {
        // Create BigDecimal
        BigDecimal one = new BigDecimal("1.00");
        BigDecimal two = new BigDecimal("2.00");
        BigDecimal three = new BigDecimal("3.00");

        // Addition ( 1.0 + 2.0 )
        BigDecimal add = one.add(two);

        // Subtraction ( 1.0 - 2.0)
        BigDecimal subtract = one.subtract(two);

        // Multiplication ( 2.0 * 3.0 )
        BigDecimal multiply = two.multiply(three);

        // Division ( 1.0 / 3.0, round to 2 decimal places )
        BigDecimal divide = one.divide(three, 2, RoundingMode.HALF_UP);

        System.out.println("더하기: " + add);
        System.out.println("빼기 : " + subtract);
        System.out.println("곱하기: " + multiply);
        System.out.println("나누기: " + divide);
    }
}

Execution result:

더하기: 3.00
빼기 : -1.00
곱하기: 6.0000
나누기: 0.33

BigDecimal Rounding Methods

BigDecimal provides the following rounding methods, including ceiling, truncation, and rounding.

In addition to specifying rounding in calculation methods, rounding can also be specified at creation time. RoundingMode

Rounding method Description
RoundingMode.CEILING Rounding mode that moves toward positive infinity
RoundingMode.DOWN Rounding mode that moves toward zero
RoundingMode.FLOOR Rounding mode that moves toward negative infinity
RoundingMode.HALF_DOWN Rounding
RoundingMode.HALF_EVEN Rounds toward the side whose last digit is even
RoundingMode.HALF_UP Rounding
RoundingMode.UNNECESSARY Rounding is unnecessary
RoundingMode.UP Rounding mode that moves away from zero

Let’s look at an example.

package com.devkuma.basic.bigdecimal;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class BigDecimalRounding {

    public static void main(String[] args) {
        // Create BigDecimal
        BigDecimal value = new BigDecimal(123.455);

        BigDecimal roundCeiling = value.setScale(2, RoundingMode.CEILING);
        BigDecimal roundDown = value.setScale(2, RoundingMode.DOWN);
        BigDecimal roundFloor = value.setScale(2, RoundingMode.FLOOR);
        BigDecimal roundHalfDown = value.setScale(2, RoundingMode.HALF_DOWN);
        BigDecimal roundHalfEven = value.setScale(2, RoundingMode.HALF_UP);
        BigDecimal roundUp = value.setScale(2, RoundingMode.UP);

        System.out.println("RoundingMode.CEILING: " + roundCeiling);
        System.out.println("RoundingMode.DOWN :" + roundDown);
        System.out.println("RoundingMode.FLOOR :" + roundFloor);
        System.out.println("RoundingMode.HALF_DOWN :" + roundHalfDown);
        System.out.println("RoundingMode.HALF_UP :" + roundHalfEven);
        System.out.println("RoundingMode.UP :" + roundUp);
    }
}

Execution result:

RoundingMode.CEILING: 123.46
RoundingMode.DOWN :123.45
RoundingMode.FLOOR :123.45
RoundingMode.HALF_DOWN :123.45
RoundingMode.HALF_UP :123.45
RoundingMode.UP :123.46

BigDecimal Sign

This introduces how to get the sign of a numeric value with BigDecimal.

package com.devkuma.basic.bigdecimal;

import java.math.BigDecimal;

public class BigDecimalSignum {

    public static void main(String[] args) {
        int plus = new BigDecimal(100).signum(); // 1
        int zero = new BigDecimal(0).signum(); // 0
        int minus = new BigDecimal(-100).signum(); // -1

        System.out.println(plus);
        System.out.println(zero);
        System.out.println(minus);
    }
}

Execution result:

1
0
-1
Input value signum() return value
100 (positive) 1
0 (zero) 0
-100 (negative) -1

Other BigDecimal Methods

The following methods are also provided.

Method Description
intValue() Returns the BigDecimal value as an int type.
longValue() Returns the BigDecimal value as a long type.
floatValue() Returns the BigDecimal value as a float type.
doubleValue() Returns the BigDecimal value as a double type.
signum() Returns the sign of the BigDecimal.
Negative: -1, zero: 0, positive: 1
max(BigDecimal a) Compares with the value of a and returns the larger number.
min(BigDecimal a) Compares with the value of a and returns the smaller number.