// There are many ways of rounding...
Math.ceil(5.5) // Answer 6, rounds up.
Math.round(5.5) // Answer 6, rounds to the closest whole number.
Math.floor(5.5) // Answer 5, rounds down.
// ceil is short for ceiling(up), floor is down...
const round = (n, d) => Number(Math.round(n + "e" + d) + "e-" + d)
round(1.005, 2) //1.01
round(1.555, 2) //1.56
The Math.round() function
returns the value of a number rounded to the nearest integer.
Math.round(x)
// If x = 0.6 it gives you 1; if x = 2.5 it gives you 3;
// if x=2.49999 it gives you 2.
Math.round( 20.49); // 20
Math.round( 20.5 ); // 21
Math.round( 42 ); // 42
Math.round(-20.5 ); // -20
Math.round(-20.51); // -21
console.log(Math.round(5.95)); // output: 6
console.log(Math.round(5.23)); // output: 5
console.log(Math.round(-15.5)); // output: -15
import java.lang.*;
public class MathDemo {
public static void main(String[] args) {
//what is Math.round(3.45)
double x = 3.45;
System.out.println("Math.round(" + x + ")=" + Math.round(x));
}
}
//the Output is 3