

Java Math.round() with Examples
source link: https://codeahoy.com/java/Math-Round-method-JI_16/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Java Math.round() with Examples
Oct 12, 2019 · 2 mins read
The java.lang.Math class that comes bundled with Java contains various methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.
Math.round(…) method is part of the Math class. It is used for rounding a decimal to the nearest integer. In mathematics, if the fractional part of the argument is greater than 0.5, it is rounded to the next highest integer. If it is less than 0.5, the argument is rounded to the next lowest integer.
Math.round Method Signature
Math.round(…) method is overloaded. Here are the method signatures:
public static int round(float a)
public static long round(double a)
Note just like all other methods on the Math class, Math.random(…) is a static
method so you can call it directly on the Math class without needing an object. The overloaded methods take either float
or double
data type returning int
or long
respectively. This saves you from casting manually.
Math.round( 42.5); // 43
Math.round Example
The following example illustrates how to use Math.round(…) method.
public class MathEx {
public static void main(String[] args) {
round();
}
private static void round() {
long res;
res = Math.round( 21.49); // 21
System.out.println(res);
res = Math.round( 32.5); // 33
System.out.println(res);
res = Math.round( 32 ); // 32
System.out.println(res);
res = Math.round(-31.5 ); // -31
System.out.println(res);
res = Math.round(-30.51); // -31
System.out.println(res);
}
}
Output
21
33
32
-31
-31
Here’s a screenshot of the code above if you’re on mobile and having trouble reading the code above.
Special cases
- 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 of 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 Integer.MAX_VALUE.
If you need more detailed information, please see Javadocs. It’s worth noting that the Math
class in Java contains several other useful methods for arithmetic, logarithms and trignometric operations.
Recommend
-
9
[Math] IEEE 754 round-off errors[Math] IEEE 754 round-off errors (The following text has been copypasted to the Math Recipes book.) The example from Wikipedia This fragment from...
-
9
Java Math.abs() with Examples Oct 12, 2019 · 3 mins read The java.lang.Mat...
-
8
Java Math.sqrt() Method with Examples Oct 12, 2019 · 2 mins read The java.l...
-
5
Java Math.pow() Method with Examples Oct 26, 2016 · 3 mins read The Math.pow() method is used for calculating a number raised to...
-
6
Java Math.floor() Method with Examples Oct 12, 2019 · 2 mins read The java....
-
10
Java Math.ceil() Method with Examples Oct 12, 2019 · 2 mins read The java....
-
12
Java Math.random() Method with Examples Oct 12, 2019 · 3 mins read The java...
-
7
Rounding in Java – Math.Round, Math.Floor, Math.CeilWe have decimal values in java, but sometimes there is a need to round them. It is important to remember which function should be used according to the requirements. So In...
-
6
Introduction Before we start, imagine what values "a" and "b" would have after running the following: Copy Code a = Math.Round(1.5); b = Math.Rou...
-
6
The New CSS Math: round() Aug 17, 2023 CSS added many new Math functions to supplement the old favorites (think calc() and the more recent clamp()). They all ultima...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK