One, Math.trunc () 1. Definition
The Math.trunc () method removes the fractional part of a number, preserving the integer portion.
2. Syntax
Math.trunc(value)
3. Example
console.log(Math.trunc(2.01)); // 2console.log(Math.trunc(2.9)); // 2console.log(Math.trunc('0.22')); // 0console.log(Math.trunc(-1.22)); // -1console.log(Math.trunc(-1.56)); // -1console.log(Math.trunc(true)); // 1console.log(Math.trunc(undefined)); // NaN
II, Math.Round () 1. Definition
The Math.Round () method returns the integer part after a number is rounded.
2. Syntax
Math.round(value)
3. Example
console.log(Math.round(2.01)); // 2console.log(Math.round(2.9)); // 3console.log(Math.round('0.22')); // 0console.log(Math.round(-1.22)); // -1console.log(Math.round(-1.56)); // -2console.log(Math.round(true)); // 1console.log(Math.round(undefined)); // NaN
Iii. Math.ceil () 1. Definition
The Math.ceil () method returns the smallest integer greater than or equal to the number, which is rounding up.
2. Syntax
Math.ceil(value)
3. Example
console.log(Math.ceil(2.01)); // 3console.log(Math.ceil(2.9)); // 3console.log(Math.ceil('0.22')); // 1console.log(Math.ceil(-1.22)); // -1console.log(Math.ceil(-1.56)); // -1console.log(Math.ceil(true)); // 1console.log(Math.ceil(undefined)); // NaN
Iv. Math.floor () 1. Definition
The Math.floor () method returns a minimum integer less than or equal to a number, which is rounded down.
2. Syntax
Math.floor(value)
3. Example
console.log(Math.floor(2.01)); // 2console.log(Math.floor(2.9)); // 2console.log(Math.floor('0.22')); // 0console.log(Math.floor(-1.22)); // -2console.log(Math.floor(-1.56)); // -2console.log(Math.floor(true)); // 1console.log(Math.floor(undefined)); // NaN
JavaScript four numerical rounding methods