Adverts

Rounding a number is something that has to be done for any number of reasons. What many people don't realize though is how many different ways that are to round a number. The most common method is called "half up" which takes any number ending in .5 and rounds it towards positive infinity. There are also methods such as "half even" which rounds to the nearest even number and "half down" which will round halves down. There are also rounding modes that will round specifically towards or away from zero, "down" and "up", and modes that will always round in one direction "ceiling" and "floor". There is also a rounding mode that will assert that the supplied number doesn't require rounding called "unnecessary".

The code below shows some examples of rounding in Java using the java.lang.math library. Rounding can also be carried out using the java.lang.Math class but there is less control over the rounding method.

package example;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;

public class Rounding {

 
private static final int numDecimalPoints = 2;

 
public Rounding() {
   
BigDecimal number = new BigDecimal( "1234.565" );
    round
( number, RoundingMode.CEILING );
    round
( number, RoundingMode.FLOOR );
    round
( number, RoundingMode.UP );
    round
( number, RoundingMode.DOWN );
    round
( number, RoundingMode.HALF_UP );
    round
( number, RoundingMode.HALF_DOWN );
    round
( number, RoundingMode.HALF_EVEN );
    round
( number, RoundingMode.UNNECESSARY );
 
}
 
 
private void round( BigDecimal number, RoundingMode mode ) {
   
System.out.println( "Number: " + number.toPlainString() );
    System.out.println
( "Precision: " + number.precision() );
    System.out.println
( "Scale: " + number.scale() );
    System.out.println
( "Rounding Mode: " + mode.toString() );
    MathContext context =
new MathContext( ( number.precision() - number.scale() + numDecimalPoints ), mode );
   
try {
     
BigDecimal result = number.round( context );
      System.out.println
( "Result: " + result.toPlainString() + "\n" );
   
} catch( ArithmeticException ae ) {
     
System.out.println( "Unable to round " + number.toPlainString() + " with method " + mode.toString() + "\n" );
   
}
  }
 
 
public static void main(String[] args) {
   
new Rounding();
 
}
}
 

Results of running this application:

Number: 1234.565
Precision: 7
Scale: 3
Rounding Mode: CEILING
Result: 1234.57
Number: 1234.565
Precision: 7
Scale: 3
Rounding Mode: FLOOR
Result: 1234.56
Number: 1234.565
Precision: 7
Scale: 3
Rounding Mode: UP
Result: 1234.57
Number: 1234.565
Precision: 7
Scale: 3
Rounding Mode: DOWN
Result: 1234.56
Number: 1234.565
Precision: 7
Scale: 3
Rounding Mode: HALF_UP
Result: 1234.57
Number: 1234.565
Precision: 7
Scale: 3
Rounding Mode: HALF_DOWN
Result: 1234.56
Number: 1234.565
Precision: 7
Scale: 3
Rounding Mode: HALF_EVEN
Result: 1234.56
Number: 1234.565
Precision: 7
Scale: 3
Rounding Mode: UNNECESSARY
Unable to round 1234.565 with method UNNECESSARY

Adverts

Donate and Help

Please support this site and
Bandwidth doesn't grow on trees y' know :o)

Adverts

Get Adsense