Rounding
JS · Numbers & Mathsyntax
Math.round(n) | Math.floor(n) | Math.ceil(n) | Math.trunc(n)example
const price = 19.872;
console.log(Math.round(price)); // 20
console.log(Math.floor(price)); // 19
console.log(Math.ceil(price)); // 20
console.log(Math.trunc(price)); // 19
console.log(Math.trunc(-3.7)); // -3 (not -4)Note trunc() simply removes decimals (towards zero). floor() rounds towards negative infinity. They differ for negative numbers: floor(-3.2) = -4 but trunc(-3.2) = -3.