How to convert a float number to the whole number in JavaScript?
There are various methods to convert float number to the whole number in JavaScript.
- Math.floor (floating argument): Round off the number passed as parameter to its nearest integer in Downward direction.
Syntax:Math.floor(value)
<script>//float value is 4.59;varx = 4.59;varz = Math.floor(x);document.write("Converted value of "+ x +" is "+ z);</script>chevron_rightfilter_noneOutput :
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)
<script>//float value is 4.59;varx = 4.59;varz = Math.ceil(x);document.write("Converted value of "+ x +" is "+ z);</script>chevron_rightfilter_noneOutput :
Converted value of 4.59 is 5
- Math.round (floating argument): Round a number to its nearest integer.
Syntax:Math.round(var);
<script>//float value is 4.59;varx = 4.59;varz = Math.round(x);document.write("Converted value of "+ x +" is "+ z);</script>chevron_rightfilter_noneOutput :
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)
<script>//float value is 4.59;varx = 4.59;varz = Math.trunc(x);document.write("Converted value of "+ x +" is "+ z);</script>chevron_rightfilter_noneOutput :
Converted value of 4.59 is 4
- parseInt (floating argument): Accept the string and convert it into an integer.
Syntax:parseInt(Value, radix)
<script>//float value is 3.54;varx = 3.54;varz = parseInt(x);document.write("Converted value of "+ x +" is "+ z);</script>chevron_rightfilter_noneOutput :
Converted value of 3.54 is 3
- double bitwise not (~~) operator: Round a number to towards zero. If the operand is a number and it’s not NaN or Infinity.
Syntax:~~value
<script>//float value is 4.59;varx = 4.59;varz = ~~x;document.write("Converted value of "+ x +" is "+ z);</script>chevron_rightfilter_noneOutput :
Converted value of 4.59 is 4
- bitwise OR (|) operator: Round a number to towards zero.
Syntax:var = value | 0;
<script>//float value is 5.67;varx = 5.67;varz = x | 0;document.write("Converted value of "+ x +" is "+ z);</script>chevron_rightfilter_noneOutput :
Converted value of 5.67 is 5
- Using shift (>>) operator: Round a number to towards zero.
Syntax:var = value >> 0;
<script>//float value is 5.63;varx = 5.63;varz = x >> 0;//it is same as we are dividing the value by 1.document.write("Converted value of "+ x +" is "+ z);</script>chevron_rightfilter_noneOutput :
Converted value of 5.63 is 5
- Using unsigned shift (>>>) operator Round a number to towards zero.
Syntax:var = value >>> 0;
<script>//float value is 5.68;varx = 5.68;//it is same as we are dividing the value by 1.varz = x >>> 0;document.write("Converted value of "+ x +" is "+ z);</script>chevron_rightfilter_noneOutput :
Converted value of 5.68 is 5
- By subtracting the fractional part
Syntax:var = val - val%1;
<script>//float value is 5.48;varx = 5.48;varz = x - x%1;document.write("Converted value of "+ x +" is "+ z);</script>chevron_rightfilter_noneOutput :
Converted value of 5.48 is 5
- Using XOR (^) operator
Syntax:var = value ^ 0;
<script>//float value is 5.49;varx = 5.49;varz = x ^ 0;document.write("Converted value of "+ x +" is "+ z);</script>chevron_rightfilter_noneOutput :
Converted value of 5.49 is 5
Recommended Posts:
- How to convert string into float in JavaScript?
- Convert a negative number to positive in JavaScript
- Convert boolean result into number/integer in JavaScript
- JavaScript | Number.MAX_VALUE & Number.MIN_VALUE with Examples
- How to convert a string into number in PHP?
- How to convert number to month name in PHP?
- Number.isNaN() In JavaScript
- Javascript | Number() Function
- JavaScript | Number isSafeInteger()
- JavaScript | Number.isInteger( ) function
- Number Guessing Game using JavaScript
- Check a number is Prime or not using JavaScript
- JavaScript | Number.isFinite() function
- Number valueOf( ) Method In JavaScript
- How to get decimal portion of a number using JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



