BigDecimal Rounding mode

In this chapter you will learn:

  1. What are the rounding mode for BigDecimal
  2. Use different rounding mode to do calculation

Rounding mode for BigDecimal

  • static int ROUND_CEILING
    Round towards positive infinity.
  • static int ROUND_DOWN
    Round towards zero.
  • static int ROUND_FLOOR
    Round towards negative infinity.
  • static int ROUND_HALF_DOWN
    Round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down.
  • static int ROUND_HALF_EVEN
    Round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor.
  • static int ROUND_HALF_UP
    Round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.
  • static int ROUND_UNNECESSARY
    No rounding is necessary.
  • static int ROUND_UP
    Rounding mode to round away from zero.

Effect of rounding mode

The following code uses different rounding mode to do calculation.

import java.math.BigDecimal;
 /*from   ja v  a  2  s . co m*/

public class Main {
 
    public static void main(String[] args) {
        BigDecimal first = new BigDecimal(1f);
        BigDecimal second = new BigDecimal(2f);
 
        BigDecimal result1 = first.divide(second, BigDecimal.ROUND_CEILING);
        BigDecimal result2 = first.divide(second, BigDecimal.ROUND_DOWN);
        BigDecimal result3 = first.divide(second, BigDecimal.ROUND_FLOOR);
        BigDecimal result4 = first.divide(second, BigDecimal.ROUND_HALF_DOWN);
        BigDecimal result5 = first.divide(second, BigDecimal.ROUND_HALF_EVEN);
        BigDecimal result6 = first.divide(second, BigDecimal.ROUND_HALF_UP);
        BigDecimal result7 = first.divide(second, BigDecimal.ROUND_UP);
 
        System.out.println(result1);
        System.out.println(result2);
        System.out.println(result3);
        System.out.println(result4);
        System.out.println(result5);
        System.out.println(result6);
        System.out.println(result7);
 
    }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. How to create Java BigDecimal
  2. How to create BigDecimal from MathContext
Home » Java Tutorial » Data Types

Java Primitive Data Types
Java byte type
Java byte type conversion
Java short type
Java short type conversion
Java int type
Java int type conversion
Java long type
Java long type conversion
Java float type
Java float type conversion
Java double type
Java double type creation and comparison
Java double type conversion
Java Automatic Type Conversion and Casting
Data type casting
Java type promotion
Java char type
Java char conversion
Java char value and its attributes
Java boolean type
Java boolean type conversion
Autoboxing and auto-unboxing
Java Array
Create an Array
Array Index and length
Multidimensional Arrays
Array examples
Array copy
Array compare
Array Binary search
Array sort
Array to List
Array fill value
Array to String
BigInteger class
BigInteger creation
BigInteger add, subtract, multiply and divide
BigInteger power and modPow
BigInteger conversion
BigInteger to String
BigInteger bit and,or,xor,test,flip,negate
BigInteger bit shift left and right
BigInteger prime value
BigDecimal
BigDecimal constants
BigDecimal Rounding mode
BigDecimal creation
BigDecimal calculation
BigDecimal convert
BigDecimal Comparison
BigDecimal to String
BigDecimal decimal point
BigDecimal precision
BigDecimal format
Currency class