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.
public class MathExample {
public static void main(String[] args) {
System.out.println( Math.round( 5 ) ); // 5
System.out.println( Math.round( 2.49 ) ); // 2
System.out.println( Math.round( 2.50 ) ); // 3
System.out.println( Math.round( 2.51 ) ); // 3
System.out.println( Math.round( -2.49 ) ); // -2
System.out.println( Math.round( -2.50 ) ); // -2
System.out.println( Math.round( -2.51 ) ); // -3
System.out.println( Math.round( 0.999 ) ); // 1
System.out.println( Math.round( 1.001 ) ); // 1
System.out.println( Math.round( -1.001 ) ); // -1
}
}
1. Documentation
Syntax |
|
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. |
2. Custom round method examples
2.1. Rounding with precision to n
places example
This section contains a custom function that shows how to round number with precision to n
places.
public class MathExample {
static double roundPrecised(double number, int precision) {
double power = Math.pow(10, precision);
return Math.round(number * power) / power;
}
public static void main(String[] args) {
// Example:
System.out.println( roundPrecised( 5 , 0 ) ); // 5.0
System.out.println( roundPrecised( 5. , 0 ) ); // 5.0
System.out.println( roundPrecised( .5, 0 ) ); // 1.0
System.out.println( roundPrecised( 1.2345, 0 ) ); // 1.0
System.out.println( roundPrecised( 1.2345, 1 ) ); // 1.2
System.out.println( roundPrecised( 1.2345, 2 ) ); // 1.23
System.out.println( roundPrecised( 1.2345, 3 ) ); // 1.235
System.out.println( roundPrecised( -1.2345, 0 ) ); // -1.0
System.out.println( roundPrecised( -1.2345, 1 ) ); // -1.2
System.out.println( roundPrecised( -1.2345, 2 ) ); // -1.23
System.out.println( roundPrecised( -1.2345, 3 ) ); // -1.234
System.out.println( roundPrecised( 12345, -1 ) ); // 12350.0
System.out.println( roundPrecised( 12345, -2 ) ); // 12300.0
System.out.println( roundPrecised( 12345, -3 ) ); // 12000.0
}
}
2.2. Round implementation example
This section contains custom round function implementation.
public class MathExample {
static double roundNumber(double value) {
if (value < 0.0) {
double rest = (value % 1.0);
if (rest < -0.5) {
rest += 1.0;
}
return value - rest;
} else {
value += 0.5;
return value - (value % 1.0);
}
}
public static void main(String[] args) {
// Example:
System.out.println( roundNumber( 5 ) ); // 5.0
System.out.println( roundNumber( 2.49 ) ); // 2.0
System.out.println( roundNumber( 2.50 ) ); // 3.0
System.out.println( roundNumber( 2.51 ) ); // 3.0
System.out.println( roundNumber( 0.999 ) ); // 1.0
System.out.println( roundNumber( 1.001 ) ); // 1.0
System.out.println( roundNumber( -2.49 ) ); // -2.0
System.out.println( roundNumber( -2.50 ) ); // -2.0
System.out.println( roundNumber( -2.51 ) ); // -3.0
System.out.println( roundNumber( -1.001 ) ); // -1.0
}
}