W3docs

How to Convert a Float Number to Whole Number in JavaScript

In this tutorial, you will read and learn detailed information about the methods used for converting a float number to a whole number with lots of examples.

There are various easy methods to convert float number to the whole number in JavaScript. Let’s discuss each of them and see examples.

Here are several built-in functions used for rounding.

Math.floor()

The <kbd class="highlighted">math.floor</kbd> unction rounds the number passed as parameter to its nearest integer in downward direction:

Javascript Math.floor method

javascript— editable

Math.ceil()

The <kbd class="highlighted">Math.ceil()</kbd> function rounds a number up to the next largest whole number or integer:

Javascript Math.ceil method

javascript— editable

Math.round()

The <kbd class="highlighted">math.round()</kbd> function returns a number value which is rounded to the nearest integer:

Javascript Math.round()method

javascript— editable

Math.trunc()

The <kbd class="highlighted">math.trunc()</kbd> function removes any fractional digits and returns the integer part of a number:

Javascript Math.trunc method

javascript— editable

parseInt()

The <kbd class="highlighted">parseInt()</kbd> function parses the string argument and returns an integer of the given radix.

Javascript parsInt method

javascript— editable

The double bitwise not (~~) Operator

The <kbd class="highlighted">~~</kbd> operator rounds a number towards 0 if the operand is a number and it’s not NaN or Infinity.

Javascript double bitwise not (~~) Operator

javascript— editable

The bitwise OR (|) Operator

The <kbd class="highlighted">OR</kbd> operator rounds a number towards 0.

Javascript bitwise OR (|) Operator

javascript— editable

The shift (>>) Operator

The <kbd class="highlighted">shift</kbd> operator rounds a number towards 0:

Javascript shift operator

javascript— editable

The unsigned shift (>>>) Operator

The <kbd class="highlighted">unsigned shift</kbd> rounds a number towards 0:

Javascript unsigned shift operator

javascript— editable

The XOR (^) Operator

Javascript XOR (^) Operato

javascript— editable

Numbers

There are two types of numbers in JavaScript: Regular and BigInt. Regular is also known as “double-precision floating-point numbers”. The math is a built-in object which has properties and methods for mathematical constants and functions. The math object works with the Number type not BigInt.