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.
Below are various methods to convert float numbers to whole numbers in JavaScript:
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
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
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
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
Round a number to its nearest integer.
Syntax:
Math.round(var);
Example: Here we are using the above-explained method to convert numbers.
javascript
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
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
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
Accept the string and convert it into an integer.
Syntax:
parseInt(Value, radix);
Example: Here is basic example of above method.
javascript
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
let x = 4.59;
let z = ~~x;
console.log( "Converted value of " + x + " is " + z);
|
Output:
Converted value of 4.59 is 4
Round a number towards zero.
Syntax:
let = value | 0;
Example: Here is an example of the above-explained method.
javascript
let x = 5.67;
let z = x | 0;
console.log( "Converted value of " + x + " is " + z);
|
Output:
Converted value of 5.67 is 5
Round a number to towards zero.
Syntax:
let = value >> 0;
Example: Basic example of shift(>>) operator.
javascript
let x = 5.63;
let z = x >> 0;
console.log( "Converted value of " + x + " is " + z);
|
Output:
Converted value of 5.63 is 5
Round a number to towards zero.
Syntax:
let = value >>> 0;
Example: Here is an example of the above-explained method.
javascript
let x = 5.68;
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
In this we substract the fractional part
Syntax:
let = val - val%1;
Example: In this example, we are using the above-explained method.
javascript
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
Syntax:
let = value ^ 0;
Example: Here is the basic use of the XOR(^) operator.
javascript
let x = 5.49;
let z = x ^ 0;
console.log( "Converted value of " + x + " is " + z);
|
Output:
Converted value of 5.49 is 5
Last Updated :
18 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...