Precise rounding of decimals using Rounding Mode Enumeration

SHARE & COMMENT :

The Rounding Mode Enum in java.math package is used to perform precise rounding of decimal values. It was introduced in Java 5.0.
This Enum provides various constants each of which is used for different modes of rounding. The decimal value would be rounded off to the number of decimal places based on the scale that is defined for the decimal value.

also read:

The Rounding Mode Enum constants are UP, DOWN, CEILING, FLOOR, HALF_DOWN, HALF_EVEN, HALF_UP, and UNNECESSARY.

UP

This rounds off any given decimal value away from zero. It basically increments the decimal value. Let us see a simple example,

BigDecimal value = new BigDecimal("2.3");
value = value.setScale(0, RoundingMode.UP);
BigDecimal value1 = new BigDecimal("-2.3");
value1 = value1.setScale(0, RoundingMode.UP);
System.out.println(value + "n" + value1);

The output of the above code is,

3
-3

In this example, we have specified the scale as 0. Hence, the rounded value does not have any decimal places.

DOWN

This rounds off any given decimal value towards zero. It does not increment the value. Let us see a simple example,

BigDecimal value = new BigDecimal("2.3");
value = value.setScale(0, RoundingMode.DOWN);
BigDecimal value1 = new BigDecimal("-2.3");
value1 = value1.setScale(0, RoundingMode.DOWN);
System.out.println(value + "n" + value1);

The output of the above code is,

2
-2

CEILING

It rounds off any decimal value towards positive infinity. For a positive decimal, it performs the same rounding as RoundingMode.UP. In case of a negative decimal, it performs the same rounding as as for RoundingMode.DOWN. It does not decrement the value. Let us see a sample,

BigDecimal value = new BigDecimal("2.3");
value = value.setScale(0, RoundingMode.CEILING);
BigDecimal value1 = new BigDecimal("-2.3");
value1 = value1.setScale(0, RoundingMode.CEILING);
System.out.println(value + "n" + value1);

The output of the above code is,

3
2

FLOOR

It rounds off any decimal value towards negative infinity. For a positive decimal, it performs the same rounding as RoundingMode.DOWN. In case of a negative decimal, it performs the same rounding as as for RoundingMode.UP. It does not increment the value. Let us see a sample that illustrates this,

BigDecimal value = new BigDecimal("2.3");
value = value.setScale(0, RoundingMode.FLOOR);
BigDecimal value1 = new BigDecimal("-2.3");
value1 = value1.setScale(0, RoundingMode.FLOOR);
System.out.println(value + "n" + value1);

The output of the above code is,

2
-3

HALF_UP

This rounds off a decimal value towards the nearest neighbouring value apart from the case when both neighbouring values are equidistant, in which case it just rounds up the value. It performs the same rounding as RoundingMode.UP if the discarded fractional part is >= 0.5, else it performs the same rounding as RoundingMode.DOWN. Let us see a sample that illustrates this,

BigDecimal value = new BigDecimal("2.4");
value = value.setScale(0, RoundingMode.HALF_UP);
BigDecimal value1 = new BigDecimal("2.5");
value1 = value1.setScale(0, RoundingMode.HALF_UP);
BigDecimal value2 = new BigDecimal("2.6");
value2 = value2.setScale(0, RoundingMode.HALF_UP);
System.out.println(value + "n" + value1 + "n" + value2);

The output of this code is,

2
3
3

HALF_DOWN

This rounds off a decimal value towards the nearest neighbouring value apart from the case when both neighbouring values are equidistant, in which case it just rounds down the value. It performs the same rounding as RoundingMode.DOWN if the discarded fractional part is > 0.5, else it performs the same rounding as RoundingMode.UP. Let us see an example for this,

BigDecimal value = new BigDecimal("2.4");
value = value.setScale(0, RoundingMode.HALF_DOWN);
BigDecimal value1 = new BigDecimal("2.5");
value1 = value1.setScale(0, RoundingMode.HALF_DOWN);
BigDecimal value2 = new BigDecimal("2.6");
value2 = value2.setScale(0, RoundingMode.HALF_DOWN);
System.out.println(value + "n" + value1 + "n" + value2);

The output of the above code is,

2
2
3

HALF_EVEN

This rounds off a decimal value towards the nearest neighbouring value apart from the case when both neighbouring values are equidistant, in which case it just rounds towards the nearest even number. It performs the same rounding as RoundingMode.HALF_UP if the digit to the left of the discarded fractional part is odd, and performs the same rounding as RoundingMode.HALF_DOWN if it’s even. Let us see an example for this,

BigDecimal value = new BigDecimal("2.4");
value = value.setScale(0, RoundingMode.HALF_EVEN);
BigDecimal value2 = new BigDecimal("2.6");
value2 = value2.setScale(0, RoundingMode.HALF_EVEN);
BigDecimal value3 = new BigDecimal("7.5");
value3 = value3.setScale(0, RoundingMode.HALF_EVEN);
BigDecimal value4 = new BigDecimal("6.5");
value4 = value4.setScale(0, RoundingMode.HALF_EVEN);
System.out.println(value + "n" + value2+ "n" + value3+ "n" + value4);

The output of the above code is,

2
3
8
6

UNNECESSARY

This Rounding mode is used to insist that a value does not need rounding, and this can be possible only when the value has no decimals. If it is used in cases where in the value has decimal places i.e. when rounding is needed, then this will throw Arithmetic Exception stating that rounding is necessary.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better