JS: Math ceiling, floor, round, truncate
Math.ceil(x)-
Return a integer that's equal or greater than x.
console.log(Math.ceil(1.3)); // 2 console.log(Math.ceil(-0.5)); // -0 Math.floor(x)-
Return a integer that's equal or less than x.
Math.round(x)-
Return a number to the nearest integer. If tie, round towards positive infinity. e.g. 0.5 rounds to 1. and -0.5 rounds to 0.
console.log(Math.round(1.3)); // 1 console.log(Math.round(1.49999)); // 1 console.log(Math.round(1.5)); // 2 console.log(Math.round(-0.3)); // -0 console.log(Math.round(-0.5)); // -0 console.log(Math.round(-0.6)); // -1 Math.trunc(x)-
(new in ECMAScript 2015)
Return the integral part of x.
console.log(Math.trunc(1.3)); // 1 console.log(Math.trunc(0.5)); // 0 console.log(Math.trunc(-0.5)); // -0