Rounding Mode of Numeric in Java


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 predefine 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 magnitude of calculated numerical value. The digit is never incremented prior to 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 digit (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 digit (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

Programing Example

import java.math.RoundingMode;

import java.text.NumberFormat;

public class RoundingDemo {

  public static void main(String[] args) {

    NumberFormat numf = NumberFormat.getNumberInstance();

    numf.setMaximumFractionDigits(2);

    numf.setRoundingMode (RoundingMode.CEILING);

    System.out.println("Default rounding mode: " + numf.getRoundingMode());

    System.out.println("523.454 rounds to " + numf.format(523.454));

    System.out.println("733.455 rounds to " + numf.format(733.455));

    System.out.println("823.456 rounds to " + numf.format(823.456));

    System.out.println("Floor  mode");

    numf.setRoundingMode (RoundingMode.FLOOR);

    System.out.println("Default rounding mode: " + numf.getRoundingMode());

    System.out.println("523.454 rounds to " + numf.format(523.454));

    System.out.println("733.455 rounds to " + numf.format(733.455));

    System.out.println("823.456 rounds to " + numf.format(823.456));

    System.out.println("HALF_DOWN mode");

    numf.setRoundingMode (RoundingMode.HALF_DOWN);

    System.out.println("Default rounding mode: " + numf.getRoundingMode());

    System.out.println("523.454 rounds to " + numf.format(523.454));

    System.out.println("733.455 rounds to " + numf.format(733.455));

    System.out.println("823.456 rounds to " + numf.format(823.456));

    System.out.println();

  }

}

OUTPUT

rounding cmd.gif

Resources

Rounded Corner Datagrid in WPF
Use of ByteStreams and CharacterStreams in JAVA
Learning JDBC (Java Database Connectivity)
Working with Hibernate - Display , Insert, Update and Delete in JAVA
Auto Complete Text Box in C#
 

Up Next
    Ebook Download
    View all
    Learn
    View all