Math Class in Java
➤ Math Class Methods
➤ Import Math Class
➤ Math.PI in Java
➤ Math.sqrt() in Java
➤ Math.pow() in Java
➤ Math.max() in Java
➤ Math.abs() in Java
➤ Math.ceil() in Java
➤ Math.floor() in Java
➤ Math.round() in Java
➤ Math.random() Java
Java.lang.Object Class
➤ Object Class in Java
➤ getClass() method
➤ hashCode() Method
➤ toString() Method
➤ Java equals() Method
➤ == vs equals() in Java
➤ HashCode() vs equals()
➤ Clone() Method in Java
➤ Object Class Quiz Set-1
➤ Object Class Quiz Set-2
➤ equals() Method Quiz
➤ Hashcode Value Quiz
Math.round() Method in Java | The java.lang.Math.round() method returns the closest value to the argument, with ties rounding to positive infinity. There are two overloaded forms of the round() method in java.lang.Math class,
public static int round(float a)
public static long round(double a)
Java Math.round() Examples
Java program to demonstrate Math.round() method
public class Test {
public static void main(String[] args) {
System.out.println(Math.round(5.3)); // 5
System.out.println(Math.round(5.5)); // 6
System.out.println(Math.round(5.7)); // 6
}
}
Output:-
5
6
6
Other examples,
Math.round(9.0) | 9 |
Math.round(9.3) | 9 |
Math.round(9.5) | 10 |
Math.round(9.7) | 10 |
Math.round(-9.0) | -9 |
Math.round(-9.3) | -9 |
Math.round(-9.5) | -9 |
Math.round(-9.7) | -10 |
If we import the Math class statically and then we can invoke the round() method without calling through its class name.
import static java.lang.Math.*;
public class Test {
public static void main(String[] args) {
System.out.println(round(5.3)); // 5
System.out.println(round(5.5)); // 6
System.out.println(round(5.7)); // 7
}
}
The “import static java.lang.Math.*;” statement will import all static members of the Math class. But if we want to import only the round() method of the Math class, not another static method and variables of the Math class then we can use the “import static java.lang.Math.round;” statement. Learn more about static import in Java
import static java.lang.Math.round;
public class Test {
public static void main(String[] args) {
System.out.println(round(5.3)); // 5
System.out.println(round(5.5)); // 6
}
}
Java Round to 2 Decimal Places
By default round() method rounds all digits after decimal place but we can write code to round float or double value to n decimal places,
double number = 12.3456789;
// rounding upto 2 decimal places
double n2 = Math.round(number * 100.0) / 100.0;
Java program to round the given value up to 1, 2, and 3 decimal places.
public class Test {
public static void main(String[] args) {
double number = 12.3456789;
System.out.println(number);
// rounding upto 1 decimal places
double n1 = Math.round(number * 10.0) / 10.0;
System.out.println(n1);
// rounding upto 2 decimal places
double n2 = Math.round(number * 100.0) / 100.0;
System.out.println(n2);
// rounding upto 3 decimal places
double n3 = Math.round(number * 1000.0) / 1000.0;
System.out.println(n3);
}
}
Output:-
12.3456789
12.3
12.35
12.346
Special Cases for Math.round() Method in Java
1) If the argument is NaN, the result is 0.
System.out.println(Math.round(Float.NaN)); // 0
System.out.println(Math.round(Double.NaN)); // 0
2) While working with “public static int round(float a)”,
- 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 of the Integer.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 of the Integer.MAX_VALUE.
public class Test {
public static void main(String[] args) {
// argument is negative infinity
System.out.println(Math.round(Float.NEGATIVE_INFINITY));
// argument is less than or equal to Integer.MIN_VALUE
System.out.println(Math.round(Integer.MIN_VALUE));
System.out.println(Math.round(Long.MIN_VALUE));
// argument is positive infinity
System.out.println(Math.round(Float.POSITIVE_INFINITY));
// argument is grater than or equal to Integer.MAX_VALUE
System.out.println(Math.round(Integer.MAX_VALUE));
System.out.println(Math.round(Long.MAX_VALUE));
System.out.println(Math.round(Float.MAX_VALUE));
}
}
Output:-
-2147483648
-2147483648
-2147483648
2147483647
2147483647
2147483647
2147483647
3) While working with “public static long round(double a)”,
- If the argument is negative infinity or any value less than or equal to the value of Long.MIN_VALUE, the result is equal to the value of Long.MIN_VALUE
- If the argument is positive infinity or any value greater than or equal to the value of Long.MAX_VALUE, the result is equal to the value of Long.MAX_VALUE.
public class Test {
public static void main(String[] args) {
// argument is negative infinity
System.out.println(Math.round(Double.NEGATIVE_INFINITY));
// argument is less than or equal to Long.MIN_VALUE
double d1= Long.MIN_VALUE;
System.out.println(Math.round(d1));
// argument is positive infinity
System.out.println(Math.round(Double.POSITIVE_INFINITY));
// argument is grater than or equal to Integer.MAX_VALUE
double d2 = Long.MAX_VALUE;
double d3 = Float.MAX_VALUE;
double d4 = Double.MAX_VALUE;
System.out.println(Math.round(d2));
System.out.println(Math.round(d3));
System.out.println(Math.round(d4));
}
}
Output:-
-9223372036854775808
-9223372036854775808
9223372036854775807
9223372036854775807
9223372036854775807
9223372036854775807
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!