In this article, we will see how to round number in Java. We will use Java built in Math.round()
method which allows us to round a floating point number to the nearest whole number or will use a few other options as well. We can find these under the java.lang.math
class
1. How to round number in java
Java provides many built-in methods to round of a number.Math.round() is one of the built in method for rounding number in Java to the nearest whole number. To put it in a simple terms, it will round a decimal value to the nearest integer or long value so 4.67 is rounded to 5 while the 3.20 will be rounded to the 3, which is the closest integer value. Let’s take this simple example in the following code:
public class Main {
public static void main(String[] args) {
double a = 4.67;
System.out.println(Math.round(a));
//output : 5
double b = 3.20;
System.out.println(Math.round(b));
//output 3
}
}
1.1. Math.round()
The round()
function has very basic syntax. If the decimal value is greater than 0.5 then the decimal value will be rounded off to its higher value and if the decimal value is less than 0.5 then it will be rounded off to its lower value.
Math.round(input)
- It takes one argument of
double
number orfloat
data types. - It will round the input to the nearest integer or long value.
- It will return
int
value for afloat
data types as input. - A
long
value will return if we passdouble
type as input value
public class Main {
public static void main(String[] args) {
//4.67 will be rounded to 5 since value is greater than 5 after decimal
double a = 4.67;
System.out.println(Math.round(a));
//output : 5
//4.50 will be rounded to 5 since value is equal to 5 after decimal
double b = 4.50;
System.out.println(Math.round(b));
//output : 5
//3.20 will be rounded to 3 since value is less than 5 after decimal
double c = 3.20;
System.out.println(Math.round(c));
//output 3
}
}
You can also change the above method to see how the Math.round()
will work with float data type and learn how to round number in Java.
public class Main {
public static void main(String[] args) {
//4.67 will be rounded to 5 since value is greater than 5 after decimal
double a = 4.67f;
System.out.println(Math.round(a));
//output : 5
//4.50 will be rounded to 5 since value is equal to 5 after decimal
double b = 4.50f;
System.out.println(Math.round(b));
//output : 5
//3.20 will be rounded to 3 since value is less than 5 after decimal
double c = 3.20f;
System.out.println(Math.round(c));
//output 3
}
}
There are other different ways which we can use to round a number in Java. Let’s look at a few of them to understand them better.
1.2. Math.ceil() method
Java Math.ceil()
method returns the smallest (closest to negative infinity) double
value that is greater than or equal to the argument and is equal to a mathematical integer. Here is the method signature for the Math.ceil()
public static double ceil(double val)
- It takes
double
primitive types as input parameter. - Returns the smallest (closest to negative infinity)
double
value that is greater than or equal to the argument and is equal to a mathematical integer.
Keep in mind the following points when using the java.lang.Math.ceil()
method
- If the value of the argument value is already equal to a mathematical integer number, then the result is the same as the argument.
- If the input is
NaN
or an infinity or positive zero or negative zero, then the result is the same as the argument. - If the argument value is less than zero but greater than -1.0, then the result is a negative zero.
Let’s look at the following example to understand it more clearly:
public class Main {
public static void main(String[] args) {
//Input: Postive value, Output: Positive ceil value of argument
double a = 34.356;
//Input: Negative value, Output: Negative ceil value of argument
double b = -54.12;
//Input: Positive 0, Output: Positive 0
double c = 0;
//Input: Negative 0, Output: Negative 0
double d = -0;
//Input: Less than zero greater than -1.0, Output: Negative 0
double e = -0.99;
System.out.println("Ceil of a: " + Math.ceil(a));
System.out.println("Ceil of b: " + Math.ceil(b));
System.out.println("Ceil of c: " + Math.ceil(c));
System.out.println("Ceil of d: " + Math.ceil(d));
System.out.println("Ceil of e: " + Math.ceil(e));
}
}
1.3. Math.floor() method
The math.floor() method of java math class returns the largest double value that is less than or equal to the argument. Here are some example for the understanding.
floor(1.2656) //1.0
floor(61.5656) //61.0
floor(-0.891) //-1.0
Here is the signature of the method
public static double floor(double val)
- Parameter – it takes a
double
value as input parameter
For the return:
- If the argument value is already equal to a mathematical integer, then the result is the same as the argument.
- If the argument is
NaN
or an infinity or positive zero or negative zero, then the result is the same as the argument.
Let’s look at this example to see how to round a number in Java using Math.floor()
.
public class Main {
public static void main(String[] args) {
//Input: Positive value, Output: largest or equal double value of input (34.0)
double a = 34.356;
//Input: Negative value, Output: largest or equal double value of input (-55.0)
double b = -54.12;
//Input: Positive 0, Output: Same as the argument(0)
double c = 0;
//Input: Negative 0, Output: Same as the argument(0)
double d = -0;
System.out.println("Floor of a: " + Math.floor(a));
System.out.println("Floor of b: " + Math.floor(b));
System.out.println("Floor of c: " + Math.floor(c));
System.out.println("Floor of d: " + Math.floor(d));
}
}
Output
Floor of a: 34.0
Floor of b: -55.0
Floor of c: 0.0
Floor of d: 0.0
1.4. Math.round() method
Another method of the Java Math built-in class to round doubles or float is Math.round()
method. There are 2 variation of this method and it takes double
or float data type as method argument and float or int as return. The result of rounding the argument to an integer is calculated by adding 1/2 (i.e., 0.5) to the number and then taking the floor of the result. This determines the nearest int value that is closer to the argument.
public static long round(double val)
public static int round(float a)
The round(float a )
takes float
as input and returns the closest int
to the argument with following exceptions:
- If the argument is NaN, the result is 0.
- If the argument is negative infinity or any value less than or equal to the value of
Integer.MIN_VALUE
, the result is equal to the value ofInteger.MIN_VALUE
. - If the argument is positive infinity or any value greater than or equal to the value of
Integer.MAX_VALUE
, the result is equal to the value ofInteger.MAX_VALUE
.
Here is full example explaining how to round number in Java using the Math.round()
method.
public class Main {
public static void main(String[] args) {
//Input: Positive value, Output: Nearest Integer (34)
float f = 34.356f;
//Input: Negative float value, Output: Nearest Integer (-54)
double f1 = -54.12f;
//Input: NAN, Output: 0
double c = 0.0/0;
//Input: Positive Infinity, Output: Integer.MAX_VALUE (9223372036854775807)
double p = 100.0/0;
System.out.println("Floor of a: " + Math.round(f));
System.out.println("Floor of b: " + Math.round(f1));
System.out.println("Floor of c: " + Math.round(c));
System.out.println("Floor of d: " + Math.round(p));
}
}
2. Using format
Apart from Java math class, we can use the format() method to learn how to round number in java? This is one of the easy method to round number to specific number of decimal places in Java. We can use this method with System.out.format()
.
We can specify different formatting options like %c%d%f
etc to format a float
number. This will output data as a print stream, which is output string.
- We can use
%f
for the floating-point number. To define specified precision for the decimal value, we can provide precision details with%f
like%.4
f. - %
d
– base 10 decimal numbers.
public class Main {
public static void main(String[] args) {
float f = 34.356f;
double f1 = -54.12f;
double c = 34.453456;
System.out.format("%.2f",f); //34.36
System.out.format("%.1f",f1); //-54.1
System.out.format("%.3f",c); //34.453
}
}
Summary
In this article, we saw how to round number in java. We saw the various methods and technique to round a number in Java. Here is a quick summary of what we learned so far.
- rounding-off can be used to covert a decimal value or float value to an integer.
- The
java.lang.Math
class provides numerous methods which helped us understand as how to round number in java. - Math.ceil() and Math.floor() explicitly convert the decimal value to higher and lower integer values.
- We can use the String format method to round of decimal values or float values to desired decimal value. This is a good option if we want to display some values, although not recommended for the complex calculations.
As always, the source for all our posts is available on our GitHub repository. You can also use our online Java compiler to run all these examples without setting up any local environment.