Round a value to integer
static long round(double a)
- Returns the closest long to a.
static int round(float a)
- Returns the closest int to a.
public class Main {
public static void main(String[] args) {
System.out.println(Math.round(1.2));
System.out.println(Math.round(1.9));
}
}
The output:
1
2
Round Java float and double numbers using Math.round
public class Main {
public static void main(String[] args) {
System.out.println(Math.round(1.3f));
System.out.println(Math.round(2.5f));
System.out.println(Math.round(2.5f));
System.out.println(Math.round(-9.4f));
System.out.println(Math.round(-3.5f));
}
}
The output:
1
3
3
-9
-3
Home
Java Book
Essential Classes
Java Book
Essential Classes
Math:
- Math class
- Constant E and PI
- Get the absolute value
- sin, cos, tan: Trigonometric functions
- Cube root and square root
- Ceilling and flooring value
- Copy sign
- Raise the power
- Get exponent
- sqrt(x2 +y2)
- IEEE remainder
- logarithm
- Max, min value
- Next value
- Math Random
- Round to int to double
- Round a value to integer
- value * 2^ scaleFactor
- Get the sign
- Degree to Radian, and Radian to Degree
- Returns the size of an ulp of the argument