How to Convert Float to Integer in JavaScript?

In JavaScript, the built-in function parseInt() converts float or string value into an integer. To begin with, let’s create a variable and assign a float value to it.

let flowerPrice = 7.80
console.log(flowerPrice);
// Output: 7.8

Above, we created a variable with a float value and print it through the console.log() function that returns the assigned value. Next, we need to check if flowerPrice data type is float. To do that we can use JavaScript’s typeof operator that helps us to know the data type of a value.

console.log(typeof flowerPrice);
// Output: number

We have the number in the console log returned by typeof operator for flowerPrice. In the next step, we need to use the parseInt() function for converting the float value into an integer.

convertIntoInteger = parseInt(flowerPrice);
console.log( convertIntoInteger);
// Output: 7

The parseInt() function converted flowerPrice variable value into an integer which is assigned to the new variable convertIntoInteger. In the console log, the convertIntoInteger variable returned an integer value which proves that the parseInt() function converted the float value into integer.


Related Posts