Math.trunc() Method with Example in JavaScript

JavaScript | Math.trunc() Method: Here, we are going to learn about the trunc() method of Math class in JavaScript with Examples.
Submitted by Shivang Yadav, on February 04, 2020

JavaScript | Math.trunc() Method

Math.trunc() is a function in math library of JavaScript that is used to extract the integer part of the given floating-point number. It removes the decimal and all the digits after it (right of it).

Syntax:

    Math.trunc(value);

Parameter(s):

  • value – represents the value which is the float from which integer values is to be extracted.

Return value:

The return type of this method is number, it returns an integer.

Values accepted: Integer, floating-point, numeric string.

Invalid Values: non-numeric string, empty variable, empty array all will return NaN (Not a Number).

Browser support: Chrome, Internet asinlorer, Mozilla, Safari, Opera mini.

Example 1: Valid values

console.log(Math.trunc(5.4));
console.log(Math.trunc(-3.87646801));
console.log(Math.trunc(0));
console.log(Math.trunc("-0.65"));

Output

5
-3
0
-0

Example 2: Invalid values

console.log(Math.trunc("Javascript"));

Output

NaN

JavaScript Math Object Methods »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.