The Math.trunc() function in JavaScript is used to return the integer part of a floating-point number by removing the fractional digits. In other words, Math.trunc() function cuts off the dot and the digits to the right of it.
Syntax:
Math.trunc(value)
Parameters: This function accepts a single parameter value . It is the floating-point number which is to be sent to the Math.trunc() function.
Return Value: The Math.trunc() function returns the integer part of the given number.
Below example illustrates the Math.trunc() function in JavaScript:
- When a positive number of float type is passed as a parameter:
<
script
type
=
"text/javascript"
>
document.write(Math.trunc(15.56));
</
script
>
Output:
15
- When a negative number of float type is passed as a parameter:
<
script
type
=
"text/javascript"
>
document.write(Math.trunc(-15.56));
</
script
>
Output:
-15
- When a positive number between 0 and 1 is passed as a parameter:
<
script
type
=
"text/javascript"
>
document.write(Math.trunc(0.236));
</
script
>
Output:
0
- When a negative number between 0 and 1 is passed as a parameter:
<
script
type
=
"text/javascript"
>
document.write(Math.trunc(-0.236));
</
script
>
Output:
0
leave a comment
0 Comments