How to Remove the Decimal Part of a Number in JavaScript ?
The decimal portion of a number in JavaScript can be removed through various methods, including the Math. trunc( ) method, Math. floor( ) method, Math. ceil( ) method, and Math. round( ) method.
Syntax
Math.trunc(11.5) // Output: 11
Math.floor(12.5) // Output: 12
Math.ceil(15.5) // Output: 15
Math.round(-20.5) // Output: -20
Table of Content
- Using Math.trunc() Method
- Using Math.floor() Method
- Using Math.ceil() Method
- Using Math.round() Method
Using Math.trunc() Method
The Javascript Math. trunc() method is used to return the integer part of a floating-point number by removing the fractional digits.
Example: The below code illustrates how to remove the decimal part of a number Using the Math. trunc( ) Method.
Javascript
console.log(Math.trunc(15.80)); console.log(Math.trunc(0.12)); console.log(Math.trunc(-10.80)); |
Output:
15
0
-10
Using Math.floor() Method
Math.floor() method is used to round off the number to its nearest integer and returns the smallest integer greater than or equal to the given number.
Example: The below code illustrates how to remove the decimal part of a number Using Math.floor() Method.
Javascript
console.log(Math.floor(-12.10)); console.log(Math.floor(0.79)); |
Output:
-13
0
Using Math.ceil() Method
Math.ceil() method is used to return the smallest integer greater than or equal to a given number.
Example: The below code illustrates how to remove the decimal part of a number Using Math.ceil() Method.
Javascript
console.log(Math.ceil(32.80)); console.log(Math.ceil(0.80)); console.log(Math.ceil(-29.70)); |
Output:
33
1
-29
Using Math.round() Method
Math.round() method is used to round a number to its nearest integer. If the fractional part of the number is greater than or equal to .5, the argument is rounded to the next higher integer.
Example: The below code illustrates how to remove the decimal part of a number.
Javascript
console.log(Math.round(42.80)); console.log(Math.round(0.80)); console.log(Math.round(-89.70)); |
Output:
43
1
-90
Contact Us