Signup/Sign In
PUBLISHED ON: AUGUST 9, 2021

How to Round a Number to N Decimal Places in Java?

Rounding a number means getting a simpler and approximate value for it. Pi is often rounded off to two or three decimal places, one-third is often written as 0.334. In Java, there are a few different ways to round off a floating-point number. Let's learn how to do this.

Rounding Using printf() Method

We can simply print the rounded number to the console using System.out.printf(). We can specify the number of decimal places that we want to round off by using precision modifiers. The following code demonstrates the use of the printf() method.

public static void main(String args[])
{
	double pi = 3.14159265359;
	double oneThird = 1.0/3.0;
	float g = 9.807f;
		
	System.out.printf("Pi: %.2f %n", pi);
	System.out.printf("One Third: %.3f %n", oneThird);
	System.out.printf("Gravitation Acceleration(g): %.0f", g);
}


Pi: 3.14
One Third: 0.333
Gravitation Acceleration(g): 10

Rounding Using DecimalFormat() Method

The DecimalFormat class can be used to define a pattern in which we want to format the number. We can specify the number of decimal places by using hashtags(#) and then we need to use the format() method to round off the digits. The code below demonstrates the working of DecimalFormat.

public static void main(String args[])
{
	double pi = 3.14159265359;
	double oneThird = 1.0/3.0;
	float g = 9.807f;
		
	DecimalFormat d1 = new DecimalFormat("#.##");
	DecimalFormat d2 = new DecimalFormat("#.###");
	DecimalFormat d3 = new DecimalFormat("#");
		
	System.out.println("Pi: " + d1.format(pi));
	System.out.println("One Third: " + d2.format(oneThird));
	System.out.print("Gravitation Acceleration(g):" + d3.format(g));
}


Pi: 3.14
One Third: 0.333
Gravitation Acceleration(g):10

If you are unsure about the digits that will be present before the decimal point then use just a single hash. The DecimalFormat() will never drop any digits before the decimal.

Rounding Using Math.round() Method

Java's Math class provides us lots of methods for performing basic mathematical operations. Math.round() is used to round decimal numbers. It can be used when we just want to round a number to zero decimal places. Remember the following three edge cases while using Math.round().

  • If the input is not a number(NaN), then Math.round() returns 0.
  • If the input is positive infinity or any value greater than Integer.MAX_VALUE, the Math.round() returns Integer.MAX_VALUE.
  • If the input is negative infinity or any value less than Integer.MIN_VALUE, the Math.round() returns Integer.MIN_VALUE.
public static void main(String args[])
{
	double pi = 3.14159265359;
	double oneThird = 1.0/3.0;
	float g = 9.807f;
		
	System.out.println("Pi: " + Math.round(pi));
	System.out.println("One Third: " + Math.round(oneThird));
	System.out.println("Gravitation Acceleration(g):" + Math.round(g));
}


Pi: 3
One Third: 0
Gravitation Acceleration(g):10

We can build our own method by using Math.round() to round more than zero decimal places. Our round() method will take a decimal value and the number of decimal places to round as parameters and will return the rounded number. The code for this is shown below. However, it is recommended to use the below code as it can give unexpected outcomes.

public static double round(double num, int places)
{
    double scale = Math.pow(10, places);
    double roundedNum = Math.round(num * scale) / scale;
    return roundedNum;
}

public static void main(String args[])
{
	double pi = 3.14159265359;
	double oneThird = 1.0/3.0;
	float g = 9.807f;
		
	System.out.println("Pi: " + round(pi, 2));
	System.out.println("One Third: " + round(oneThird, 3));
	System.out.println("Gravitation Acceleration(g):" + round(g, 0));
}


Pi: 3.14
One Third: 0.333
Gravitation Acceleration(g):10.0

Rounding Using BigDecimal

We can use the round() method of the BigDecimal class to round a number. The following code demonstrates how to round a decimal number using BigDecimal's round() method.

public static void main(String args[])
{
	BigDecimal pi = new BigDecimal("3.14159265359");
	pi = pi.setScale(4, RoundingMode.HALF_UP);
		
	System.out.println("Pi: " + pi);
}


Pi: 3.1416

If the number is not of BigDecimal type, then we can first convert it and then round it. The following method can be used to round off numbers of double data types.

public static double round(double num, int places)
{
	BigDecimal b = new BigDecimal(Double.toString(num));
	b = b.setScale(places, RoundingMode.HALF_UP);
	return b.doubleValue();
}
	
public static void main(String args[])
{
	double pi = 3.14159265359;
	double oneThird = 1.0/3.0;
	double g = 9.807f;
		
	pi = round(pi, 4);
	oneThird = round(oneThird, 3);
	g = round(g, 0);
		
	System.out.println("Pi: " +pi);
	System.out.println("One Third: " + oneThird);
	System.out.println("Gravitation Acceleration(g):" + g);
}


Pi: 3.1416
One Third: 0.333
Gravitation Acceleration(g):10.0

Rounding Using DoubleRounder

The DoubleRounder is a utility class that belongs to the decimal4j library. It can be used to round numbers but sometimes it gives unexpected results. For example, when rounding 256.025 to two decimal places, it returns 256.02 instead of 256.03.

public static void main(String args[])
{
	double g = 9.807f;
	System.out.println("Gravitation Acceleration(g):" + DoubleRounder.round(g, 1);
}


Gravitation Acceleration(g):9.8

Summary

Java provides us with quite a few ways of rounding decimals. We learned how to format a decimal number using printf() and DecimalFormat(). We also used the Math.round() method and BigDecimal class to round a number.



About the author:
I am a 3rd-year Computer Science Engineering student at Vellore Institute of Technology. I like to play around with new technologies and love to code.