Converting Floats to Integers in JavaScript: 10 Effective Ways

Find out the different methods to achieve JavaScript float to int conversion effectively.

Do you want to know how to convert float numbers to integers in JavaScript? All you have to do is keep reading this comprehensive guide.

But before we explore into the solutions of converting floats to integers. Let’s first understand the floating point numbers and integers.

What are floating point numbers?

Floating-point numbers, commonly known as floats, are numbers that contain a fractional part.

In JavaScript, these numbers are represented using the number data type and can have decimals.

For example, 4.14 and 0.75 are floating-point numbers

What are Integers?

Integers are whole numbers without any fractional parts. They are also represented using the number data type in JavaScript.

Examples of integers include 8, 10, and -16.

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

JavaScript offers several methods to convert float to int, each with its own advantages and use cases.

Let’s explore these methods:

Use parseInt()

One of the simplest ways to convert a float to an int is by using the parseInt() function. This function parses a string argument and returns an integer.

Syntax

parseInt(string, radix(optional))

To convert a float, we can pass the floating-point number as a string to the parseInt() function.

For example:

let floatValue = 1.14;
let intValue = parseInt(floatValue);
console.log(intValue);

Output:

1

Use Math.floor()

The Math.floor() function rounds down a float to the nearest integer, making it an effective method for converting floats to integers.

Syntax

Math.floor(number)

Here’s an example:

let floatValue = 2.99;
let intValue = Math.floor(floatValue);
console.log(intValue);

Output:

2

Use Math.ceil()

The Math.ceil() function rounds up a float to the nearest integer, which can be helpful in certain situations.

Syntax

Math.ceil(val)

Here’s an example:

let floatValue = 2.5;
let intValue = Math.ceil(floatValue);
console.log(intValue);

Output:

3

Use Math.round()

The Math.round() function rounds a float to the nearest integer. If the decimal part is .5 or greater, it rounds up; otherwise, it rounds down.

Syntax

Math.round(number)

Here’s an example:

let floatValue = 3.9;
let intValue = Math.round(floatValue);
console.log(intValue); 

Output:

4

Here’s another example:

let floatValue = 3.3;
let intValue = Math.round(floatValue);
console.log(intValue); 

Output:

3

Use Bitwise OR (|) Operator

Another unique approach to convert float to int involves using the bitwise OR operator (|). This method truncates the decimal part and gives the integer value.

Syntax

number | 0

Here’s an example:

let floatValue = 5.8;
let intValue = floatValue | 0;
console.log(intValue);

Output:

5

Use Math.trunc()

This function removes any fractional digits and returns the integer part of a number.

Syntax

Math.trunc(number)

Here’s an example:

let num = 6.7;
let result = Math.trunc(num);
console.log(result);

Output:

6

Use Bitwise NOT (~~)

This operator inverts all the bits in your number and in the process converts the number to an int!

Syntax

~~number

Here’s an example:

let num = 7.7;
let result = ~~num;
console.log(result);

Output:

7

Use Bitwise Right Shift (>>)

This operator shifts the bits of the first operand to the right by the number of positions specified by the second operand and returns a result of the same type as the first operand.

Syntax

 number >> 0

Here’s an example:

let num = 8.8;
let result = num >> 0;
console.log(result); 

Output:

8

Use Unsigned Right Shift (>>>)

This operator shifts the bits of the first operand to the right by the number of positions specified by the second operand and returns a result of the same type as the first operand, but with all leading zeros filled with zeros instead of ones (as in a signed right shift).

Syntax

 number >>> 0

Here’s an example:

let num = 9.99;
let result = num >>> 0;
console.log(result);

Output:

9

Use Modulo (%)

This operator returns the remainder of dividing two numbers. You can use it to remove any fractional part from a float by subtracting it from itself modulo 1: (x – x % 1).

Syntax

 (number - number % 1)

Here’s an example:

let num = 10.10;
let result = (num - num % 1);
console.log(result);

Output:

10

Dealing with rounding errors in floating-point conversions

When working with floating-point numbers, it is important to be aware of rounding errors that may occur during conversions.

These errors are due to the inherent imprecision of floating-point representations.

To mitigate such errors, consider using the toFixed() method or custom rounding functions that adjust the number of decimal places before conversion.

Here’s an example code:

let floatValue = 1.2345;
let roundedValue = floatValue.toFixed(2); // Rounded to 2 decimal places
let intValue = parseInt(roundedValue);
console.log(intValue); 

Output:

1

Conclusion

In conclusion, this article provides various methods for converting float numbers to integers in JavaScript.

We discussed the concepts of floating-point numbers and integers and provided the different effective methods along with example codes and output for better understanding when converting data types.

We are hoping that this article provides you with enough information that help you understand the JavaScript float to int.

You can also check out the following article:

Thank you for reading itsourcecoders 😊.

Leave a Comment