JavaScript Tutorial JavaScript References

JavaScript - Math.trunc() Method



The JavaScript Math.trunc() method is used to round the given number towards zero. It returns the nearest integral value with absolute value less than the argument.

Syntax

Math.trunc(x)

Parameters

x Specify a number.

Return Value

Returns the nearest integral value with absolute value less than the argument.

Example:

In the example below, Math.trunc() method returns the nearest integral value with absolute value less than the argument.

var txt;

txt = "Math.trunc(10.2) = " + Math.trunc(10.2) + "<br>";
txt = txt + "Math.trunc(10.5) = " + Math.trunc(10.5) + "<br>";
txt = txt + "Math.trunc(10.8) = " + Math.trunc(10.8) + "<br>";
txt = txt + "Math.trunc(-5.2) = " + Math.trunc(-5.2) + "<br>";
txt = txt + "Math.trunc(-5.5) = " + Math.trunc(-5.5) + "<br>";
txt = txt + "Math.trunc(-5.8) = " + Math.trunc(-5.8) + "<br>";

The output (value of txt) after running above script will be:

Math.trunc(10.2) = 10
Math.trunc(10.5) = 10
Math.trunc(10.8) = 10
Math.trunc(-5.2) = -5
Math.trunc(-5.5) = -5
Math.trunc(-5.8) = -5

❮ JavaScript - Math Object