How to round a number to n decimal places in Java?

How to cut the double decimal point?

How to round off a float to two decimal places?

How to round double to two decimal places?

There are two ways to do rounding operation in java using Math class and another is using BigDecimal class.

Math class has three methods which helps for rounding operation first one is Math.round which returns the closest int or long value of the value we have passed.

Math ceil and floor returns the double value. Ceil method returns the smallest double value which is not less than the argument and is equal to a mathematical integer. Floor do exactly opposite by returning the largest double value which is not greater than the argument and is equal to a mathematical integer.

If we want to keep some decimal numbers then we need to use BigDecimal class, BigDecimal class allows to cut the decimal numbers we want. BigDecimal have method setScale which gives complete control over the rounding operation by providing option to specify scale and rounding mode. We can specify the scale as per we want to have how much decimal numbers. Rounding modes allow user how he want to round the decimal numbers, it has options like ROUND_UP , ROUND_CEILING, ROUND_FLOOR, ROUND_HALF_UP, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_UNNECESSARY , in example below we can see

public class RoundOffTest {

/** Creates a new instance of RoundOffTest */

public RoundOffTest() {

}

public static void main(String args[]){

double doubleVal = 3.343678;

float floatVal = 2.456f;

// Rounding off operations without decimal places using Math.round

int roundInt = Math.round(floatVal);

long roundLong = Math.round(doubleVal);

System.out.println("Integer value with round operation : "+roundInt);

System.out.println("Long value with round operation : "+roundLong);

double ceilValue = Math.ceil(doubleVal);

System.out.println("Double value with ceil operation : "+ceilValue);

double floorValue = Math.floor(doubleVal);

System.out.println("Double value with floor operation: "+floorValue);

// Now suppose we want to have decimals numbers but we want to cut the number

// then we can use BigDecimal class

int decimalPlaces = 2;

BigDecimal bdTest = new BigDecimal( doubleVal );

bdTest = bdTest.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP);

double bdVal = bdTest.doubleValue();

System.out.println("ROUND_HALF_UP Test");

System.out.println("Double value using BigDecimal: "+bdVal);

bdTest = new BigDecimal( doubleVal );

bdTest = bdTest.setScale(3, BigDecimal.ROUND_CEILING);

bdVal = bdTest.doubleValue();

System.out.println("ROUND_CEILING Test");

System.out.println("Double value using BigDecimal: "+bdVal);

bdTest = new BigDecimal( doubleVal );

bdTest = bdTest.setScale(3, BigDecimal.ROUND_FLOOR);

bdVal = bdTest.doubleValue();

System.out.println("ROUND_FLOOR Test");

System.out.println("Double value using BigDecimal: "+bdVal);

}

}

Integer value with round operation : 2

Long value with round operation : 3

Double value with ceil operation : 4.0

Double value with floor operation: 3.0

ROUND_HALF_UP Test

Double value using BigDecimal: 3.34

ROUND_CEILING Test

Double value using BigDecimal: 3.344

ROUND_FLOOR Test

Double value using BigDecimal: 3.343

Ah just what I'm looking for! I could not find this ooprater documented on the Groovy site. Excellent topics too btw, makes my learning idiomatic Groovy way easier. Thanks.

ReplyDelete