Categories

# How to Convert a Floating-Point Number to a Whole Number in JavaScript?

Converting a floating-point number to a whole number is something that we’ve to do sometimes in JavaScript apps.

In this article, we’ll look at how to convert a JavaScript floating-point number to a whole number.

### `Math.floor`

The `Math.floor` method lets us round a number down to the nearest integer.

For instance, we can use it by writing:

``````const intvalue = Math.floor(123.45);
console.log(intvalue)
``````

Then we get 123 as the value of `intValue` .

### `Math.ceil`

The `Math.ceil` method lets us round a number up to the nearest integer.

For instance, we can write:

``````const intvalue = Math.ceil(123.45);
console.log(intvalue)
``````

Then `intValue` is 124.

### `Math.round`

The `Math.round` method lets us round a number down to the nearest integer if the first decimal digit is 4 or lower.

Otherwise, it rounds the number up to the nearest integer.

For instance, if we have:

``````const intvalue = Math.round(123.45);
console.log(intvalue)
``````

Then `intValue` is 123.

### `Math.trunc`

The `Math.trunc` method lets us return the integer part of a number by removing the fractional digits.

For example, we can write:

``````const intvalue = Math.trunc(123.45);
console.log(intvalue)
``````

Then `intValue` is 123.

### `parseInt`

The `parseInt` function lets us convert a floating-point number to an integer.

It works like `Math.trunc` in that it removes the fractional digits from the returned number.

For example, we can write:

``````const intvalue = parseInt(123.45);
console.log(intvalue)
``````

And `intValue` is 123.

To make sure that we return a decimal number, we pass 10 into the 2nd argument:

``````const intvalue = parseInt(123.45, 10);
console.log(intvalue)
``````

### Conclusion

JavaScript provides a few functions and methods to lets us convert floating-point numbers to integers. ## By John Au-Yeung

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