Rounding Mode of Numeric in Java

In this article we are going to describe a special behavior of numerical operations capable of discarding precision.

Introduction

 
In this article, we are going to describe a special behavior of numerical operations capable of discarding precision. In each, the rounding mode indicates how the least significant digit of a rounded result is to be calculated. If a fewer number of significant digits are returned then the digits needed to exact numerical result, and the discarded digits will be referred to as the discard function regardless of the digit's contribution to the value of the number.
 
Each rounding mode description includes a table listing how different two-digit decimal values would round to a one-digit decimal value under the rounding mode in question. The result column in the tables could be determined by creating a BigDecimal number with the specified value, forming a MathContext object with the proper settings (precision set to 1, and the roundingMode set to the rounding mode in question), and calling round on this number with the proper MathContext. A summary table showing the results of these rounding operations for all rounding modes appears below.
 

There are some predefined constants and their meaning are listed below

 
1: UP
 
In this Rounding mode, the numerical value is always rounded to zero. And in this mode the current magnitude is never decreased; any numerical value is always increased.
 
Example
 
Input Number Input rounded to one digit
 
with UP rounding
7.5 8
1.5 2
3.6 4
3.1 4
3.0 3
-3.0 -3
-3.1 -4
-3.6 -4
-3.5 -4
-7.5 -8
 
2: DOWN
 
In this Rounding mode, the numerical value rounds toward zero. And never incremented in the magnitude of calculated numerical value. The digit is never incremented before a discarded fraction.
 
Example
 
Input Number Input rounded to one digit
 
with UP rounding
7.5 7
1.5 1
3.6 3
3.1 3
3.0 3
-3.0 -3
-3.1 -3
-3.6 -3
-3.5 -3
-7.5 -7
 
3: CEILING
 
In this rounding mode, the behavior of CEILING is different for positive and negative integers. If the result is positive, then it behaves as for RoundingMode.UP; if negative, behaves as for RoundingMode.DOWN. But in this rounding mode, the calculated values are never decreased.
 
Example
 
Input Number Input rounded to one digit
 
with UP rounding
7.5 8
1.5 2
3.6 4
3.1 4
3.0 3
-3.0 -3
-3.1 -3
-3.6 -3
-3.5 -3
-7.5 -7
 
4: FLOOR
 
This rounding mode rounds towards negative infinity. The numerical value of the calculated result is less than the absolute calculated value. If the result is positive, then it behaves as for RoundingMode.DOWN; if negative, it behaves as for RoundingMode.UP.
 
Example
 
Input Number Input rounded to one digit
 
with UP rounding
7.5 7
1.5 1
3.6 3
3.1 3
3.0 3
-3.0 -3
-3.1 -4
-3.6 -3
-3.5 -4
-7.5 -8
 
5: HALF_UP
 
In this rounding mode, the rounding is towards the "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. In the case of HALF_UP, both are possible and the calculated result may be less or greater.
 
Example
 
Input Number Input rounded to one digit
 
with UP rounding
7.5 8
1.5 2
3.6 4
3.1 3
3.0 3
-3.0 -3
-3.1 -3
-3.6 -4
-3.5 -4
-7.5 -8
 
6: HALF_DOWN
 
In this rounding mode, the rounding is done towards "nearest digit (neighbor)" unless both digits (neighbor) 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.
 
Example
 
Input Number Input rounded to one digit
 
with UP rounding
7.5 7
1.5 1
3.6 4
3.1 3
3.0 3
-3.0 -3
-3.1 -3
-3.6 -4
-3.5 -3
-7.5 -7
 
7: HALF_EVEN
 
In this rounding mode, the rounding is towards "nearest digit (neighbor)" unless both digits (neighbor) are equidistant in which case, the rounding mode round towards the even neighbor. the Behaves of HALF_EVEN rounding mode like 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.
 
Example
 
Input Number Input rounded to one digit
 
with UP rounding
7.5 8
1.5 1
3.6 3
1.1 1
3.0 3
-3.0 -3
-3.1 -3
-3.6 -4
-2.5 -2
-7.5 -8
 
8: UNNECESSARY
 
In this Rounding mode to assert (Exception generate) that the requested operation has an exact result, hence no rounding is necessary. If this rounding mode is specified on an operation that yields an inexact result, an ArithmeticException is thrown.
 
Example
 
Input Number Input rounded to one digit
 
with UP rounding
7.5  throw ArithmeticException
1.5 throw ArithmeticException 
3.6 throw ArithmeticException
3.1 throw ArithmeticException
3.0 3
-3.0 -3
-3.1 throw ArithmeticException
-3.6  throw ArithmeticException
-3.5 throw ArithmeticException
-7.5 throw ArithmeticException
 
Programming Example
  1. import java.math.RoundingMode;  
  2. import java.text.NumberFormat;  
  3. public class RoundingDemo {  
  4.   public static void main(String[] args) {  
  5.     NumberFormat numf = NumberFormat.getNumberInstance();  
  6.     numf.setMaximumFractionDigits(2);  
  7.     numf.setRoundingMode (RoundingMode.CEILING);  
  8.     System.out.println("Default rounding mode: " + numf.getRoundingMode());  
  9.     System.out.println("523.454 rounds to " + numf.format(523.454));  
  10.     System.out.println("733.455 rounds to " + numf.format(733.455));  
  11.     System.out.println("823.456 rounds to " + numf.format(823.456));  
  12.     System.out.println("Floor  mode");  
  13.     numf.setRoundingMode (RoundingMode.FLOOR);  
  14.     System.out.println("Default rounding mode: " + numf.getRoundingMode());  
  15.     System.out.println("523.454 rounds to " + numf.format(523.454));  
  16.     System.out.println("733.455 rounds to " + numf.format(733.455));  
  17.     System.out.println("823.456 rounds to " + numf.format(823.456));  
  18.     System.out.println("HALF_DOWN mode");  
  19.     numf.setRoundingMode (RoundingMode.HALF_DOWN);  
  20.     System.out.println("Default rounding mode: " + numf.getRoundingMode());  
  21.     System.out.println("523.454 rounds to " + numf.format(523.454));  
  22.     System.out.println("733.455 rounds to " + numf.format(733.455));  
  23.     System.out.println("823.456 rounds to " + numf.format(823.456));  
  24.     System.out.println();  
  25.   }  
  26. }  
OUTPUT
 
rounding cmd.gif
 
Resources