Introduction
Rounding a number means getting an approximate value of it. In Java, there are several ways to round off a floating-point number. In this lab, we will learn how to round numbers using different methods in Java.
Rounding a number means getting an approximate value of it. In Java, there are several ways to round off a floating-point number. In this lab, we will learn how to round numbers using different methods in Java.
We can use the System.out.printf()
method to print the rounded number to the console. We can specify the number of decimal places that we want to round off using precision modifiers.
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); // prints Pi: 3.14
System.out.printf("One Third: %.3f %n", oneThird); // prints One Third: 0.333
System.out.printf("Gravitation Acceleration(g): %.0f", g); // prints Gravitation Acceleration(g): 10
}
To run the code, open the terminal and go to the directory where the code file is located. Then, compile the code using javac filename.java
. After that, run it using java filename
.
We can use the DecimalFormat
class to specify a pattern in which we want to format the number. We can use hashtags (#) to specify the number of decimal places, and then we can use the format()
method to round off the digits.
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)); // prints Pi: 3.14
System.out.println("One Third: " + d2.format(oneThird)); // prints One Third: 0.333
System.out.println("Gravitation Acceleration(g):" + d3.format(g)); // prints Gravitation Acceleration(g): 10
}
To run the code, open the terminal and go to the directory where the code file is located. Then, compile the code using javac filename.java
. After that, run it using java filename
.
We can use Math.round()
to round decimal numbers. It can be used when we want to round a number to zero decimal places.
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)); // prints Pi: 3
System.out.println("One Third: " + Math.round(oneThird)); // prints One Third: 0
System.out.println("Gravitation Acceleration(g):" + Math.round(g)); // prints Gravitation Acceleration(g): 10
}
To run the code, open the terminal and go to the directory where the code file is located. Then, compile the code using javac filename.java
. After that, run it using java filename
.
We can create our own method to round a decimal number if we want to round to more than zero decimal places. The following method takes a decimal value and the number of decimal places to round as parameters and returns the rounded number.
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)); // prints Pi: 3.14
System.out.println("One Third: " + round(oneThird, 3)); // prints One Third: 0.333
System.out.println("Gravitation Acceleration(g):" + round(g, 0)); // prints Gravitation Acceleration(g): 10.0
}
To run the code, open the terminal and go to the directory where the code file is located. Then, compile the code using javac filename.java
. After that, run it using java filename
.
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 the BigDecimal
class's round()
method.
public static void main(String args[]) {
BigDecimal pi = new BigDecimal("3.14159265359");
pi = pi.setScale(4, RoundingMode.HALF_UP); // Rounds to 4 decimal places
System.out.println("Pi: " + pi); // prints Pi: 3.1416
}
To run the code, open the terminal and go to the directory where the code file is located. Then, compile the code using javac filename.java
. After that, run it using java filename
.
If the number is not of the BigDecimal
type, we can first convert it and then round it using the BigDecimal
class. 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); // prints Pi: 3.1416
System.out.println("One Third: " + oneThird); // prints One Third: 0.333
System.out.println("Gravitation Acceleration(g):" + g); // prints Gravitation Acceleration(g): 10.0
}
To run the code, open the terminal and go to the directory where the code file is located. Then, compile the code using javac filename.java
. After that, run it using java filename
.
In this lab, we have learned different methods to round decimal numbers in Java. We used printf()
and DecimalFormat()
for simpler rounding, Math.round()
for rounding to zero decimal places, BigDecimal
for precise rounding, and DoubleRounder
for rounding using the decimal4j
library. We also created a custom method to round any decimal number to any number of decimal places.