Skip to content
the poor coder

javascript convert float to int

Poor Man
Published
Reading time
2 min read
On this page

How to convert a float to an integer using JavaScript?#

Converting a float to an integer in JavaScript is a common task that you may encounter while working with numbers. In this post, we will discuss various ways to convert a float to an integer in JavaScript.

Method 1: Using parseInt()#

The parseInt() function in JavaScript can be used to convert a floating-point number to an integer.

Text

let floatNum = 3.14;
let intNum = parseInt(floatNum);
console.log(intNum);

The above code will output:

Text

3

Here, the parseInt() function takes the floatNum variable as input and returns an integer value of 3. Note that if the floatNum variable has a decimal value of .5 or greater, parseInt() will round up to the nearest integer.

Method 2: Using Math.floor()#

The Math.floor() function in JavaScript can be used to round down a floating-point number to the nearest integer.

Text

let floatNum = 3.75;
let intNum = Math.floor(floatNum);
console.log(intNum);

The above code will output:

Text

3

Here, the Math.floor() function takes the floatNum variable as input and returns an integer value of 3. Note that Math.floor() always rounds down to the nearest integer.

Method 3: Using Bitwise Operators#

Another way to convert a float to an integer is by using bitwise operators. The following code converts a float to an integer using the bitwise OR operator:

Text

let floatNum = 3.14;
let intNum = floatNum | 0;
console.log(intNum);

The above code will output:

Text

3

Here, the bitwise OR operator performs a type conversion on the floatNum variable and returns an integer value of 3.

Method 4: Using Math.trunc()#

The Math.trunc() function in JavaScript can be used to remove the decimal part of a floating-point number and return the integer value.

Text

let floatNum = 3.75;
let intNum = Math.trunc(floatNum);
console.log(intNum);

The above code will output:

Text

3

Here, the Math.trunc() function takes the floatNum variable as input and returns an integer value of 3. Note that Math.trunc() always truncates the decimal part of a floating-point number.

Conclusion#

In this post, we discussed various ways to convert a float to an integer in JavaScript. You can choose any method depending on your use case and preferences.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC