Convert float to nearest integer in JavaScript
This post will discuss how to convert float to nearest integer in JavaScript.
There are several ways to convert a float to the nearest integer in JavaScript, depending on how we want to round the decimal part. Here are some of the most common functions:
1. Using Math object
The Math object provides various mathematical functions that are useful in converting a float to an integer. They can be used by calling them with a number as an argument, like this: Math.function(number).
- We can use the
Math.round()function, which rounds a float to the nearest integer, following the standard mathematical rules. - We can use the
Math.floor()orMath.ceil()functions to round a float to the nearest integer. The difference is that floor rounds down, while ceil rounds up. - We can use the
Math.trunc()function, which removes the fractional part of a number and returns the integer part. However, this function is only available in ECMAScript 6 or later.
The following code illustrates this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// float variables let x = 3.14; let y = 3.55; // rounds a float to the nearest integer using round console.log(Math.round(x)); // 3 console.log(Math.round(y)); // 4 // rounds a float down to the nearest integer using floor console.log(Math.floor(x)); // 3 console.log(Math.floor(y)); // 3 // rounds a float up to the nearest integer using ceil console.log(Math.ceil(x)); // 4 console.log(Math.ceil(y)); // 4 // removes the fractional part of the float using trunc console.log(Math.trunc(x)); // 3 console.log(Math.trunc(y)); // 3 |
2. Using parseInt() function
The parseInt() function parses a string argument and returns an integer of the specified radix (base). We can use this function to convert a float to an integer by passing it as a string argument and specifying the radix as 10 for decimal numbers. For example, to convert the float 3.14 to an integer, we can use:
|
1 2 3 4 5 6 7 8 |
// A float variable let floatNum = 3.14; // Convert the float to an integer using parseInt let intNum = parseInt(floatNum, 10); // Prints 3 console.log(intNum); |
3. Using bitwise operators
Bitwise operators perform operations on the binary representation of numbers, and can be used to convert a float to an integer by truncating the fractional part. For example, we can use the bitwise OR operator (|) with zero as the second operand to convert a float to an integer. For example, to convert the float 3.14 to an integer, we can use:
|
1 2 3 4 5 6 7 8 |
// A float variable let floatNum = 3.14; // Convert the float to an integer using bitwise OR let intNum = floatNum | 0; // Prints 3 console.log(intNum); |
The bitwise operators are fast and concise ways of converting a float to an integer, but they may not work as expected for large numbers or negative numbers.
That’s all about converting float to nearest integer in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)