How to round off decimal numbers to integers in JavaScript.

There are many methods in JavaScript to convert a decimal number into integers. We discuss some unique methods I've come across. Those are,
Ceil
Floor
Trunc
Round
toFixed
To Fixed:
Apart from toFixed, all options are related to the Math.js. So, first, we look at the toFixed method. Below, we can see the toFixed method accepts one parameter to decide the accuracy of the decimal number. It will return the whole decimal number if we keep the parameter empty.
let test = 1.89338;
console.log(test.toFixed(2)); //1.89
console.log(test.toFixed()); // 2
Ceil & Round:
Similarly, Math. ceil() provides the same answer while converting it into an integer. But to get a two-decimal precision, we need to do something extra.
let test = 1.89338;
console.log(Math.Ceil(test)); // 2
let rounded = Math.Ceil(test *100) / 100 //Math.round(test*100) / 100
console.log(rounded) //1.89
First, we multiply the given number by 100 and divide the Math.Ceil() output by 100. It's also the same for Math. round() too.
Trunc and Floor:
Rounding off the number into decimal precision is the same as Ceil and Round. Unlike other methods trunc and floor return the smallest value while rounding off. These methods round down even if the first decimal number is more than 5.
let test = 1.89778;
console.log(Math.trunc(test)) //1
let truncated = Math.trunc(test * 100) / 100;
console.log(truncated) // 1.89
Short Story:
These are the major methods for rounding off a decimal number into an integer. Mostly, we use Math. round but, other methods are also used based on the needs and requirements.
Ceil, toFixed(), and Round return the nearest integer in an upward direction. Both truncate and floor methods return the nearest integer in a downward direction.


