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:

Javascript Math.floor method
let x = 5.49; let z = Math.floor(x); console.log("Converted value of " + x + " is " + z);

Math.ceil()

The Math.ceil() function rounds a number up to the next largest whole number or integer:

Javascript Math.ceil method
let x = 5.49; let z = Math.ceil(x); console.log("Converted value of " + x + " is " + z);

Math.round()

The math.round() function returns a number value which is rounded to the nearest integer:

Javascript Math.round()method
let x = 5.49; let z = Math.round(x); console.log("Converted value of " + x + " is " + z);

Math.trunc()

The math.trunc() function removes any fractional digits and returns the integer part of a number:

Javascript Math.trunc method
let x = 5.49; let z = Math.trunc(x); console.log(+z);

parseInt()

The parseInt() function parses the string argument and returns an integer of the given radix.

Javascript parsInt method
let x = 1.54; let z = parseInt(x); console.log(+z);

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.

Javascript double bitwise not (~~) Operator
let x = 5.49; let z = ~~x; console.log(+z);

The bitwise OR (|) Operator

The OR operator rounds a number towards 0.

Javascript bitwise OR (|) Operator
let x = 4.67; let z = x | 0; console.log(+z);

The shift (>>) Operator

The shift operator rounds a number towards 0:

Javascript shift operator
let x = 4.63; let z = x >> 0; //it is same as we are dividing the value by 1. console.log(+z);

The unsigned shift (>>>) Operator

The unsigned shift rounds a number towards 0:

Javascript unsigned shift operator
let x = 5.68; let z = x >>> 0; console.log(+z);

The XOR (^) Operator

Javascript XOR (^) Operato
let x = 4.49; let z = x ^ 0; console.log(+z);

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.