JavaScript Math.trunc() | Get integer part of a number

The JavaScript Math.trunc() method is used when we need to find the integer part of a number. For example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>

  <p id="xyz"></p>

  <script>
    let num = 137.4356;
    document.getElementById("xyz").innerHTML = Math.trunc(num);
  </script>
   
</body>
</html>
Output

JavaScript Math.trunc() Syntax

The syntax of Math.trunc() method in JavaScript is:

Math.trunc(x)

The parameter x is required, refers to number.

The Math.trunc() method returns the integer part of specified number x.

Other methods to do similar job

Here are the list of methods that are used to either round a number in other way, or to do similar type of job.

  • Math.ceil() - round a number up to its nearest largest integer.
  • Math.floor() - round a number up to its nearest smallest integer.
  • Math.round() - round a specified number to its nearest integer value.
  • toFixed() - round a number to particular number of decimals.
  • toPrecision() - format a number to specific length.

JavaScript Online Test


« Previous Tutorial Next Tutorial »



Like/Share Us on Facebook 😋