EN
Java - Math.round() method example
0 points
Math.round
is a static method that returns a number that is rounded to the closest integer number.
xxxxxxxxxx
1
public class MathExample {
2
3
public static void main(String[] args) {
4
System.out.println( Math.round( 5 ) ); // 5
5
6
System.out.println( Math.round( 2.49 ) ); // 2
7
System.out.println( Math.round( 2.50 ) ); // 3
8
System.out.println( Math.round( 2.51 ) ); // 3
9
10
System.out.println( Math.round( -2.49 ) ); // -2
11
System.out.println( Math.round( -2.50 ) ); // -2
12
System.out.println( Math.round( -2.51 ) ); // -3
13
14
System.out.println( Math.round( 0.999 ) ); // 1
15
System.out.println( Math.round( 1.001 ) ); // 1
16
System.out.println( Math.round( -1.001 ) ); // -1
17
}
18
}
Syntax |
xxxxxxxxxx 1 package java.lang; 2 3 public final class Math { 4 5 public static long round(double number) { ... } 6 public static int round(float number) { ... } 7 8 }
|
Parameters | number - primitive value. |
Result | Rounded number value (primitive value). |
Description | round is a static method that returns a number that is rounded to the closest integer number. |
This section contains a custom function that shows how to round number with precision to n
places.
xxxxxxxxxx
1
public class MathExample {
2
3
static double roundPrecised(double number, int precision) {
4
double power = Math.pow(10, precision);
5
return Math.round(number * power) / power;
6
}
7
8
public static void main(String[] args) {
9
// Example:
10
11
System.out.println( roundPrecised( 5 , 0 ) ); // 5.0
12
System.out.println( roundPrecised( 5. , 0 ) ); // 5.0
13
System.out.println( roundPrecised( .5, 0 ) ); // 1.0
14
15
System.out.println( roundPrecised( 1.2345, 0 ) ); // 1.0
16
System.out.println( roundPrecised( 1.2345, 1 ) ); // 1.2
17
System.out.println( roundPrecised( 1.2345, 2 ) ); // 1.23
18
System.out.println( roundPrecised( 1.2345, 3 ) ); // 1.235
19
20
System.out.println( roundPrecised( -1.2345, 0 ) ); // -1.0
21
System.out.println( roundPrecised( -1.2345, 1 ) ); // -1.2
22
System.out.println( roundPrecised( -1.2345, 2 ) ); // -1.23
23
System.out.println( roundPrecised( -1.2345, 3 ) ); // -1.234
24
25
System.out.println( roundPrecised( 12345, -1 ) ); // 12350.0
26
System.out.println( roundPrecised( 12345, -2 ) ); // 12300.0
27
System.out.println( roundPrecised( 12345, -3 ) ); // 12000.0
28
}
29
}
This section contains custom round function implementation.
xxxxxxxxxx
1
public class MathExample {
2
3
static double roundNumber(double value) {
4
if (value < 0.0) {
5
double rest = (value % 1.0);
6
if (rest < -0.5) {
7
rest += 1.0;
8
}
9
return value - rest;
10
} else {
11
value += 0.5;
12
return value - (value % 1.0);
13
}
14
}
15
16
public static void main(String[] args) {
17
// Example:
18
19
System.out.println( roundNumber( 5 ) ); // 5.0
20
21
System.out.println( roundNumber( 2.49 ) ); // 2.0
22
System.out.println( roundNumber( 2.50 ) ); // 3.0
23
System.out.println( roundNumber( 2.51 ) ); // 3.0
24
25
System.out.println( roundNumber( 0.999 ) ); // 1.0
26
System.out.println( roundNumber( 1.001 ) ); // 1.0
27
28
System.out.println( roundNumber( -2.49 ) ); // -2.0
29
System.out.println( roundNumber( -2.50 ) ); // -2.0
30
System.out.println( roundNumber( -2.51 ) ); // -3.0
31
32
System.out.println( roundNumber( -1.001 ) ); // -1.0
33
}
34
}