How to Convert a Float Number to Whole Number in JavaScript
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 math.floor unction rounds the number passed as parameter to its nearest integer in downward direction:
Math.ceil()
The Math.ceil() function rounds a number up to the next largest whole number or integer:
Math.round()
The math.round() function returns a number value which is rounded to the nearest integer:
Math.trunc()
The math.trunc() function removes any fractional digits and returns the integer part of a number:
parseInt()
The parseInt() function parses the string argument and returns an integer of the given radix.
The double bitwise not (~~) Operator
The ~~ operator rounds a number towards 0 if the operand is a number and it’s not NaN or Infinity.
The bitwise OR (|) Operator
The OR operator rounds a number towards 0.
The shift (>>) Operator
The shift operator rounds a number towards 0:
The unsigned shift (>>>) Operator
The unsigned shift rounds a number towards 0:
The XOR (^) Operator
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.