JavaScript

How to convert a float number to a whole number in JavaScript?

Many programming languages all over the globe utilize various data types to be able to differentiate between the variety of data stored inside variables. Even when it comes to numbers, there are many types. Some common examples are int, float, and double. This is not the case when dealing with JavaScript. In JavaScript, there are no different types of numbers. They are all stored the same. Whether it’s “5”, “5.3”, or even “0.00032”. But sometimes, these decimal numbers need to get rounded to whole numbers, for the sake of making calculations easier.

In this article, knowledge will be provided on how these floating type numbers can be converted into whole numbers using JavaScript.

How a float number is converted into a whole number?

JavaScript provides a wide variety of methods through which anyone can convert a floating point number into a whole number. This section will shed some light on some of the most commonly used methods.

Method 1: Using parseInt()

The most commonly used method is the “parseInt()” function. This function takes in a variable or value and converts it to int type. The syntax for it is as follows:

parseInt(x);

Check out the example for “parseInt()” below:

Method 2: Using toFixed()

The “toFixed()” method is very similar to the “parseInt()” method except it only accepts number values and will round off your number to the nearest decimal number that you specify. So for the whole number, we pass the default index which is 0 in the function. The syntax is given below:

x.toFixed();

Here x is the float number that should be converted and the function does not take any integer as an argument which means it will use the default. The sample snippet is shown below:

The next output shows rounding off to the next whole number:

Method 3: Using Math.floor()

The Math library is extremely useful when it comes to performing tasks like this. The Math.floor() function will take the decimal value and round it to the lowest whole number. The syntax of is shown down below:

Math.floor(x);

An example for this function is given below:

Method 4: Using Math.ceil()

This function will perform the opposite task to the Math.floor() function. This will round the decimal number to the highest possible whole number. Check out the syntax for this function:

Math.ceil(x);

Sample for the Math.ceil() function is presented below:

Method 5: Using Math.round()

Unlike the two Math functions used above, Math.round() makes use of the decimal number and rounds to the nearest whole number. Meaning that if the decimal number is greater than or equal to 5, it will get rounded to the next number otherwise, it is rounded to the lower number. The syntax for Math.round() is shown below:

Math.round(x);

For further understanding take a look at these two snippets:

The example above clearly shows how the Math.round() differs from the other Math functions since it rounds up the decimal into a whole number.

Conclusion

Any decimal number can be converted into a whole number in JavaScript using functions like parseInt(), toFixed(), Math.floor(), Math.ceil(), and Math.round(). This article has provided all the various ways in which any decimal number can be changed to a whole number using JavaScript.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.