Sometimes, we want to convert a double to an int in JavaScript without rounding.
In this article, we’ll look at how to convert a double to an int in JavaScript without rounding.
How to convert a double to an int in JavaScript without rounding?
To convert a double to an int in JavaScript without rounding, we can use the parseInt
function.
For instance, we write
const num = 2.9;
console.log(parseInt(num, 10));
to call parseInt
with num
and radix 10 to return an integer part of num
as the value.
As a result, we see 2 logged from the console log.
Conclusion
To convert a double to an int in JavaScript without rounding, we can use the parseInt
function.