Categories
JavaScript Answers

How to convert a double to an int in JavaScript without rounding?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *