Math.trunc()
Unlike Math.floor(), Math.ceil() and Math.round(), Math.trunc() method removes fractional part and gives out only integer part. It doesn't care about rounding the number to the nearest integer. It doesn't even notice whether the value is positive or negative. Its function is only to chop up the fractional part.
syntax
Math.trunc(x);
parameters - Math.trunc() takes a number as an argument and truncates the fractional part.
In the following example, Math.trunc() method truncated the fractional values of the provided positive numbers.
Example
<html> <body> <script> var val1 = Math.trunc("1.414"); var val2 = Math.trunc("0.49"); var val3 = Math.trunc("8.9"); document.write(val1); document.write("</br>"); document.write(val2); document.write("</br>"); document.write(val3); </script> </body> </html>
Output
1 0 8
In the following Math.trunc() function truncated the fractional values of negative numbers and displayed their integer values in the output.
Example
<html> <body> <script> var val1 = Math.trunc("-1.414"); var val2 = Math.trunc("-0.49"); var val3 = Math.trunc("-8.9"); document.write(val1); document.write("</br>"); document.write(val2); document.write("</br>"); document.write(val3); </script> </body> </html>
Output
-1 0 -8