JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

Precise rounding of decimals using Rounding Mode Enumeration

October 9, 2007 by Krishna Srinivasan Leave a 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:

  • New Features in Java 5.0
  • Generics in Java 5.0
  • Annotations in Java 5.0

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,
[code lang=”java”]
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);
[/code]
The output of the above code is,
[code lang=”java”]
3
-3
[/code]
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,
[code lang=”java”]
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);
[/code]
The output of the above code is,
[code lang=”java”]
2
-2
[/code]

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,
[code lang=”java”]
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);
[/code]
The output of the above code is,
[code lang=”java”]
3
2
[/code]

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,
[code lang=”java”]
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);
[/code]
The output of the above code is,
[code lang=”java”]
2
-3
[/code]

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,
[code lang=”java”]
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);
[/code]

The output of this code is,
[code lang=”java”]
2
3
3
[/code]

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,
[code lang=”java”]
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);
[/code]
The output of the above code is,
[code lang=”java”]
2
2
3
[/code]

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,
[code lang=”java”]
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);
[/code]
The output of the above code is,

[code]
2
3
8
6
[/code]

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.

Filed Under: Java Tagged With: Java 5.0

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.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved