Friday, September 5, 2025
HomeLanguagesJavascriptHow to convert a float number to the whole number in JavaScript...

How to convert a float number to the whole number in JavaScript ?

In this article, we will convert a float number to a whole number in Javascript. There are various methods to convert float numbers to whole numbers in JavaScript.

We will see these methods one by one now.

Math.floor (floating argument): Round off the number passed as a parameter to its nearest integer in the Downward direction

Syntax:

Math.floor(value)

Example: In this example, we are using the above-explained method.

javascript




//float value is 4.59;
let x = 4.59;
let z = Math.floor(x);
console.log("Converted value of " + x + " is " + z);


Output:

Converted value of 4.59 is 4

Math.ceil (floating argument): Return the smallest integer greater than or equal to a given number. 

Syntax:

Math.ceil(value)

Example: Here is the basic use of the Math.ceil() method.

javascript




//float value is 4.59;
let x = 4.59;
let z = Math.ceil(x);
console.log("Converted value of " + x + " is " + z);


Output:

Converted value of 4.59 is 5

Math.round (floating argument): Round a number to its nearest integer. 

Syntax:

Math.round(var);

Example: Here we are using the above-explained method to convert numbers.

javascript




//float value is 4.59;
let x = 4.59;
let z = Math.round(x);
console.log("Converted value of " + x + " is " + z);


Output:

Converted value of 4.59 is 5

Math.trunc (floating argument): Return the integer part of a floating-point number by removing the fractional digits

Syntax:

Math.trunc(value)

Example: Here we are using Math.trunc() method to remove the fractional digits.

javascript




//float value is 4.59;
let x = 4.59;
let z = Math.trunc(x);
console.log("Converted value of " + x + " is " + z);


Output:

Converted value of 4.59 is 4

parseInt (floating argument): Accept the string and convert it into an integer. 

Syntax:

parseInt(Value, radix)

Example: Here is basic example of above method.

javascript




//float value is 3.54;
let x = 3.54;
let z = parseInt(x);
console.log("Converted value of " + x + " is " + z);


Output:

Converted value of 3.54 is 3

double bitwise not (~~) operator: Round a number to zero. If an operand is a number and it’s not NaN or Infinity. 

Syntax:

~~value

Example: Here is the example of a not(~~) operator.

javascript




//float value is 4.59;
let x = 4.59;
let z = ~~x;
console.log("Converted value of " + x + " is " + z);


Output:

Converted value of 4.59 is 4

JavaScript bitwise OR (|) Operator: Round a number towards zero. 

Syntax:

let = value | 0;

Example: Here is an example of the above-explained method.

javascript




//float value is 5.67;
let x = 5.67;
let z = x | 0;
console.log("Converted value of " + x + " is " + z);


Output:

Converted value of 5.67 is 5

Using shift (>>) operator: Round a number to towards zero. 

Syntax:

let = value >> 0;

Example: Basic example of shift(>>) operator.

javascript




//float value is 5.63;
let x = 5.63;
let z = x >> 0;
//it is same as we are dividing the value by 1.
console.log("Converted value of " + x + " is " + z);


Output:

Converted value of 5.63 is 5

Using unsigned shift (>>>) operator Round a number to towards zero. 

Syntax:

let = value >>> 0;

Example: Here is an example of the above-explained method.

javascript




//float value is 5.68;
let x = 5.68;
//it is same as we are dividing the value by 1.
let z = x >>> 0;
console.log("Converted value of " + x + " is " + z);


Output:

Converted value of 5.68 is 5

By subtracting the fractional part Syntax:

let = val - val%1;

Example: In this example, we are using the above-explained method.

javascript




//float value is 5.48;
let x = 5.48;
let z = x - x % 1;
console.log("Converted value of " + x + " is " + z);


Output:

Converted value of 5.48 is 5

Using XOR (^) operator Syntax:

let = value ^ 0;

Example: Here is the basic use of the XOR(^) operator.

javascript




//float value is 5.49;
let x = 5.49;
let z = x ^ 0;
console.log("Converted value of " + x + " is " + z);


Output:

Converted value of 5.49 is 5
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11863 POSTS0 COMMENTS
Shaida Kate Naidoo
6750 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6701 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS