How to Round BigDecimal Value in Java

Tags: BigDecimal RoundingMode

In this Java core tutorial we learn how to round a java.math.BigDecimal with different rounding mode in Java programming language.

Table of contents

  1. Round BigDecimal with RoundingMode.UP mode
  2. Round BigDecimal with RoundingMode.DOWN mode
  3. Round BigDecimal with RoundingMode.CEILING mode
  4. Round BigDecimal with RoundingMode.FLOOR mode
  5. Round BigDecimal with RoundingMode.HALF_UP mode
  6. Round BigDecimal with RoundingMode.HALF_DOWN mode
  7. Round BigDecimal with RoundingMode.HALF_EVEN mode

Round BigDecimal with RoundingMode.UP mode

RoundingMode.UP is the rounding mode to round away from zero. Always increments the digit prior to a non-zero discarded fraction. We learn how to use the rounding mode UP with example Java code as below.

RoundBigDecimalExample1.java

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

public class RoundBigDecimalExample1 {
    public static void main(String... args) {
        BigDecimal bigDecimalValue1 = new BigDecimal("123.2211");
        BigDecimal bigDecimalValue2 = new BigDecimal("123.2277");

        // Round BigDecimal value with RoundingMode.UP
        bigDecimalValue1 = bigDecimalValue1.setScale(2, RoundingMode.UP);
        bigDecimalValue2 = bigDecimalValue2.setScale(2, RoundingMode.UP);

        System.out.println("bigDecimalValue1: " + bigDecimalValue1);
        System.out.println("bigDecimalValue2: " + bigDecimalValue2);
    }
}
The output as below.
bigDecimalValue1: 123.23
bigDecimalValue2: 123.23

Round BigDecimal with RoundingMode.DOWN mode

RoundingMode.DOWN is the rounding mode to round towards zero. Never increments the digit prior to a discarded fraction (i.e., truncates). We learn how to use the rounding mode DOWN with example Java code as below.

RoundBigDecimalExample2.java

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

public class RoundBigDecimalExample2 {
    public static void main(String... args) {
        BigDecimal bigDecimalValue1 = new BigDecimal("123.2211");
        BigDecimal bigDecimalValue2 = new BigDecimal("123.2277");

        // Round BigDecimal value with RoundingMode.DOWN
        bigDecimalValue1 = bigDecimalValue1.setScale(2, RoundingMode.DOWN);
        bigDecimalValue2 = bigDecimalValue2.setScale(2, RoundingMode.DOWN);

        System.out.println("bigDecimalValue1: " + bigDecimalValue1);
        System.out.println("bigDecimalValue2: " + bigDecimalValue2);
    }
}
The output as below.
bigDecimalValue1: 123.22
bigDecimalValue2: 123.22

Round BigDecimal with RoundingMode.CEILING mode

RoundingMode.CEILING is the rounding mode to round towards positive infinity. If the result is positive, behaves as for RoundingMode.UP, if negative, behaves as for RoundingMode.DOWN. We learn how to use the rounding mode CEILING with example Java code as below.

RoundBigDecimalExample3.java

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

public class RoundBigDecimalExample3 {
    public static void main(String... args) {
        BigDecimal bigDecimalValue1 = new BigDecimal("123.2211");
        BigDecimal bigDecimalValue2 = new BigDecimal("123.2277");
        BigDecimal bigDecimalValue3 = new BigDecimal("-123.2211");
        BigDecimal bigDecimalValue4 = new BigDecimal("-123.2277");

        // Round BigDecimal value with RoundingMode.CEILING
        bigDecimalValue1 = bigDecimalValue1.setScale(2, RoundingMode.CEILING);
        bigDecimalValue2 = bigDecimalValue2.setScale(2, RoundingMode.CEILING);
        bigDecimalValue3 = bigDecimalValue3.setScale(2, RoundingMode.CEILING);
        bigDecimalValue4 = bigDecimalValue4.setScale(2, RoundingMode.CEILING);

        System.out.println("bigDecimalValue1: " + bigDecimalValue1);
        System.out.println("bigDecimalValue2: " + bigDecimalValue2);
        System.out.println("bigDecimalValue3: " + bigDecimalValue3);
        System.out.println("bigDecimalValue4: " + bigDecimalValue4);
    }
}
The output as below.
bigDecimalValue1: 123.23
bigDecimalValue2: 123.23
bigDecimalValue3: -123.22
bigDecimalValue4: -123.22

Round BigDecimal with RoundingMode.FLOOR mode

RoundingMode.FLOOR is the rounding mode to round towards negative infinity. If the result is positive, behaves as for RoundingMode.DOWN, if negative, behaves as for RoundingMode.UP. We learn how to use the rounding mode FLOOR with example Java code as below.

RoundBigDecimalExample4.java

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

public class RoundBigDecimalExample4 {
    public static void main(String... args) {
        BigDecimal bigDecimalValue1 = new BigDecimal("123.2211");
        BigDecimal bigDecimalValue2 = new BigDecimal("123.2277");
        BigDecimal bigDecimalValue3 = new BigDecimal("-123.2211");
        BigDecimal bigDecimalValue4 = new BigDecimal("-123.2277");

        // Round BigDecimal value with RoundingMode.FLOOR
        bigDecimalValue1 = bigDecimalValue1.setScale(2, RoundingMode.FLOOR);
        bigDecimalValue2 = bigDecimalValue2.setScale(2, RoundingMode.FLOOR);
        bigDecimalValue3 = bigDecimalValue3.setScale(2, RoundingMode.FLOOR);
        bigDecimalValue4 = bigDecimalValue4.setScale(2, RoundingMode.FLOOR);

        System.out.println("bigDecimalValue1: " + bigDecimalValue1);
        System.out.println("bigDecimalValue2: " + bigDecimalValue2);
        System.out.println("bigDecimalValue3: " + bigDecimalValue3);
        System.out.println("bigDecimalValue4: " + bigDecimalValue4);
    }
}
The output as below.
bigDecimalValue1: 123.22
bigDecimalValue2: 123.22
bigDecimalValue3: -123.23
bigDecimalValue4: -123.23

Round BigDecimal with RoundingMode.HALF_UP mode

RoundingMode.HALF_UP is the rounding mode to round towards nearest neighbor unless both neighbors are equidistant, in which case round up. Behaves as for RoundingMode.UP if the discarded fraction is ≥ 0.5, otherwise, behaves as for RoundingMode.DOWN. We learn how to use the rounding mode HALF_UP with example Java code as below.

RoundBigDecimalExample5.java

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

public class RoundBigDecimalExample5 {
    public static void main(String... args) {
        BigDecimal bigDecimalValue1 = new BigDecimal("123.2211");
        BigDecimal bigDecimalValue2 = new BigDecimal("123.2277");

        // Round BigDecimal value with RoundingMode.HALF_UP
        bigDecimalValue1 = bigDecimalValue1.setScale(2, RoundingMode.HALF_UP);
        bigDecimalValue2 = bigDecimalValue2.setScale(2, RoundingMode.HALF_UP);

        System.out.println("bigDecimalValue1: " + bigDecimalValue1);
        System.out.println("bigDecimalValue2: " + bigDecimalValue2);
    }
}
The output as below.
bigDecimalValue1: 123.22
bigDecimalValue2: 123.23

Round BigDecimal with RoundingMode.HALF_DOWN mode

RoundingMode.HALF_DOWN is the rounding mode to round towards nearest neighbor unless both neighbors are equidistant, in which case round down. Behaves as for RoundingMode.UP if the discarded fraction is > 0.5, otherwise, behaves as for RoundingMode.DOWN. We learn how to use the rounding mode HALF_DOWN with example Java code as below.

RoundBigDecimalExample6.java

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

public class RoundBigDecimalExample6 {
    public static void main(String... args) {
        BigDecimal bigDecimalValue1 = new BigDecimal("123.2211");
        BigDecimal bigDecimalValue2 = new BigDecimal("123.2277");

        // Round BigDecimal value with RoundingMode.HALF_DOWN
        bigDecimalValue1 = bigDecimalValue1.setScale(2, RoundingMode.HALF_DOWN);
        bigDecimalValue2 = bigDecimalValue2.setScale(2, RoundingMode.HALF_DOWN);

        System.out.println("bigDecimalValue1: " + bigDecimalValue1);
        System.out.println("bigDecimalValue2: " + bigDecimalValue2);
    }
}
The output as below.
bigDecimalValue1: 123.22
bigDecimalValue2: 123.23

Round BigDecimal with RoundingMode.HALF_EVEN mode

RoundingMode.HALF_EVEN is the rounding mode to round towards the nearest neighbor unless both neighbors are equidistant, in which case, round towards the even neighbor. Behaves as for RoundingMode.HALF_UP if the digit to the left of the discarded fraction is odd; behaves as for RoundingMode.HALF_DOWN if it’s even. We learn how to use the rounding mode HALF_EVEN with example Java code as below.

RoundBigDecimalExample7.java

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

public class RoundBigDecimalExample7 {
    public static void main(String... args) {
        BigDecimal bigDecimalValue1 = new BigDecimal("5.5");
        BigDecimal bigDecimalValue2 = new BigDecimal("2.5");
        BigDecimal bigDecimalValue3 = new BigDecimal("1.6");
        BigDecimal bigDecimalValue4 = new BigDecimal("1.1");

        // Round BigDecimal value with RoundingMode.HALF_EVEN
        bigDecimalValue1 = bigDecimalValue1.setScale(0, RoundingMode.HALF_EVEN);
        bigDecimalValue2 = bigDecimalValue2.setScale(0, RoundingMode.HALF_EVEN);
        bigDecimalValue3 = bigDecimalValue3.setScale(0, RoundingMode.HALF_EVEN);
        bigDecimalValue4 = bigDecimalValue4.setScale(0, RoundingMode.HALF_EVEN);

        System.out.println("bigDecimalValue1: " + bigDecimalValue1);
        System.out.println("bigDecimalValue2: " + bigDecimalValue2);
        System.out.println("bigDecimalValue3: " + bigDecimalValue3);
        System.out.println("bigDecimalValue4: " + bigDecimalValue4);
    }
}
The output as below.
bigDecimalValue1: 6
bigDecimalValue2: 2
bigDecimalValue3: 2
bigDecimalValue4: 1

Happy Coding 😊

How to Negate BigDecimal Value in Java

How to Get Absolute of BigDecimal Value in Java

Java Convert BigDecimal to BigInteger

Java Convert BigInteger to BigDecimal

Java Convert long to BigDecimal

Java Convert int to BigDecimal

Java Convert double to BigDecimal

Java Convert float to BigDecimal

How to use BigDecimal in Java by Examples

How to Add two BigDecimal values in Java

How to Subtract two BigDecimal values in Java

How to Multiply two BigDecimal values in Java

How to Divide two BigDecimal values in Java

Java Convert BigDecimal value into double value

Java Convert BigDecimal value into float value

Java Convert BigDecimal value into int value

Java Convert BigDecimal value into long value

Java Convert BigDecimal value into short value

Java Convert BigDecimal value to byte value

Java Convert BigDecimal to String

Java Convert String to BigDecimal

Java Compare two BigDecimal values