English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Math.trunc()The method returns the integer part of the number by removing any decimal.
is different from the other three Math methods:Math.floor(),Math.ceil()andMath.round()The working principle of Math.trunc() is very simple. Regardless of whether the parameter is a positive number or a negative number, it will truncate the decimal point and digits on the right.
Note: The Math.trunc() method will not round up the number/Round down to the nearest integer, just removing the decimal.
The parameters passed to this method are implicitly converted to the number type.
Because trunc() is a static method of Math, you always use it asMath.trunc(),not used as a method of the created Math object.
Math.trunc(x)
Math.trunc(13.37); // 13 Math.trunc(42.84); // 42 Math.trunc(0.123); // 0 Math.trunc(-0.123);// -0 Math.trunc('-1.123'); // -1 Math.trunc(NaN); // NaN Math.trunc('Hello'); // NaNTest See‹/›
The numbers in the table specify the first browser version that fully supports the Math.trunc() method:
Method | |||||
Math.trunc() | 38 | 25 | 25 | 8 | 12 |
Parameter | Description |
---|---|
x | Number |
Return Value: | Integer Part of Given Number |
---|---|
JavaScript Version: | ECMAScript 6 |