The JavaScript truncate function (trunc(x)) is a Math function. The trunc function removes the decimal values from specified expressions and returns the integer value.
JavaScript trunc Syntax
The syntax of the math trunc method is:
Math.trunc(number);
Parameter: The number can be a decimal or a numerical expression.
Return Value
- If the number argument is positive or negative, the trunc function will return the truncated value.
- If it is not a number, the trunc function will return NaN; if it is Null, it will return Zero.
JavaScript trunc function Example
The truncate Function removes the decimal values and returns the integer value. In this example, we will find the truncated values of different data types using trunc() and display the output.
- First, we used the truncate function on the Positive Decimal values.
- In the next two statements, we used the JavaScript function on negative Decimal numbers.
- Next, we tried on the string text. It will return NaN (Not a Number) as output.
- Last, we tried the trunc math method on a null value, which returns zero.
<!DOCTYPE html>
<html>
<head>
<title> JavaScriptTRUNCATEFunction </title>
</head>
<body>
<h1> JavaScriptTRUNCFunction </h1>
<p id = "Pos"></p>
<p id = "Neg"></p>
<p id = "Dec"></p>
<p id = "Neg_Dec"></p>
<p id = "Str"></p>
<p id = "Null"></p>
<script>
document.getElementById("Pos").innerHTML = Math.trunc(10.95);
document.getElementById("Dec").innerHTML = Math.trunc(10.05);
document.getElementById("Neg").innerHTML = Math.trunc(-14.95);
document.getElementById("Neg_Dec").innerHTML = Math.trunc(-14.05);
document.getElementById("Str").innerHTML = Math.trunc("JavaScript");
document.getElementById("Null").innerHTML = Math.trunc(null);
</script>
</body>
</html>

JavaScript truncate decimal to 2 places
In the following example, we defined a function. Inside it, first multiply the given number by 100 and apply the trunc() function to the result. Next, divide the truncated value by 100 to get the two decimal places.
TIP: To truncate the number to 3 decimal places, replace 100 with 1000.
function sam(n) {
return Math.trunc(n * 100) / 100;
}
console.log(sam(100.5689));
console.log(sam(150.5432));
console.log(sam(10.456));
console.log(sam(20.5));
100.56
150.54
10.45
20.5
JavaScript Math.trunc negative numbers
The trunc() function supports and truncates the negative numbers. By default, it removes the decimal portion from a given negative floating-point number and moves towards zero (not negative infinity).
From the example below, you can observe that the Math.trunc() function drops the fractional point (decimal values) and prints the integer part along with the negative symbol.
console.log(Math.trunc(-10.9));
console.log(Math.trunc(-20.2));
console.log(Math.trunc(-0.5));
-10
-20
-0
What does Math.trunc() return for NaN or undefined?
If we pass NaN (not a number) or undefined as the trunc() function parameter, it returns NaN as the output.
console.log(Math.trunc(NaN));
console.log(Math.trunc(undefined));
NaN
NaN
Related Math functions