JavaScript Math trunc() Method
The Math.trunc()
method in JavaScript returns the integer part of a number by removing its fractional part. It essentially truncates the decimal portion of a number towards zero, resulting in an integer.
For positive numbers, it's equivalent to flooring the number, while for negative numbers, it's akin to ceiling the number.
Syntax:
Math.trunc(value)
Parameters:
This method accepts a single parameter as mentioned above and described below:
- value. It is the floating-point number that is to be sent to the Math.trunc() function.
Return Value:
The Math.trunc() method returns the integer part of the given number. More codes for the above method are as follows:
Example 1: Truncating Positive Number
When a positive float number passed as a parameter
console.log(Math.trunc(15.56));
Output
15
Example 2: Truncating Negative Number
When a negative number of a float type is passed as a parameter
console.log(Math.trunc(-15.56));
Output
-15
Example 3: Passing Number between 0 and 1.
Here, we are passing number between 0 and 1.
console.log(Math.trunc(0.236));
Output
0
We have a complete list of Javascript Math Objects methods, to check those please go through this Javascript Math Object Complete reference article.
Supported Browsers:
- Chrome 51
- Edge 15
- Firefox 54
- Safari 10
- Opera 38