Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Math.trunc

Drops the fractional part of a number.

Available since: ES2015

Syntax

Math.trunc(x)

Parameters

ParameterTypeDescription
xnumberThe number whose fractional part is dropped.

Return value

numberThe integer part of the number.

Examples

The integer part:

console.log(Math.trunc(7.9));

Output:

7

The difference from Math.floor on negative numbers:

console.log(Math.trunc(-7.9));
console.log(Math.floor(-7.9));

Output:

-7
-8

Notes

Math.trunc() just cuts off everything after the point, while Math.floor() always rounds down. For positive numbers the result is the same.

See also

  • Math.floor — Rounds a number down to an integer.
  • Math.ceil — Rounds a number up to an integer.
  • parseInt — Converts a string to an integer.

Related recipes

In the tutorial

  • Numbers — Numbers in JavaScript — integers and fractions, rounding, the Math object, toFixed and the special values Infinity and NaN.

Thanks for reading!

Donate to the site — we work hard for you.