AppDividend
Latest Code Tutorials

# JavaScript Math trunc() Function Example

JavaScript Math trunc() is an inbuilt method that is used to return an integral part of any number by removing the part after the decimal point(including the decimal point). The trunc() method is different from the ceil() or floor() methods as it does not round up the value to the nearest integer rather just remove the part after the decimal point, but can be implemented with the help of these methods (as demonstrated in the polyfill section).

## JavaScript Math trunc()

JavaScript, trunc() is a function used to return the integer portion of a number. It truncates the number and removes all fractional digits. Since trunc() is a static method of Math, it can be used without creating an object.

### Syntax

```Math.trunc(x)
```

### Parameter(s)

The floating-point number whose integral part is to be returned.

### Return Value

The integral part of x by removing the part after the decimal point.

### Polyfill

Polyfill is code that can be used in place of this method for browsers which do not support this method.

```Math.trunc = function(x){
x = +x;
if(!isFinite(x)) return x;
return (x – x%1) || (x<0 ? -0 : x === 0 ? x : 0);
};
```

OR

```Math.trunc = function(x){
return x<0 ? Math.ceil(x) : Math.floor(x);
};
```

See the following figure.

### Note

1. If the passed value is not a valid number, the method returns NaN.
2. If the passed value is null, the method returns 0.
3. For negative values, this method gives the same output as Math.ceil()
4. For positive values, this method gives the same output as Math.floor()

### Compatibility(Version and above)

2. Firefox v25
3. Edge v12
4. Opera v25
5. Safari v8
6. Android webview v38
7. Chrome for Android v38
8. Firefox for Android v25
9. Opera for Android v25
10. Safari on iOS v8
11. Samsung Internet v3.0
12. Node.js v0.12

Not compatible with: Internet Explorer

JavaScript version: ECMAScript 6

Consider the following examples:

The following code shows the use of the js trunc() method.

```// app.js

let a = 12.25;
let b = -6.3;
let c = 0.35;
let d = -0.2;
let e = [4.3]; // array with single element

console.log(Math.trunc(a));
console.log(Math.trunc(b));
console.log(Math.trunc(c));
console.log(Math.trunc(d));
console.log(Math.trunc(e));
```

#### Output

```node example1
12
-6
0
-0
4
```

### Javascript trunc(): NaN is returned

The following example shows the cases where NaN is returned.

```// app.js

let a = "Hello, world"; 		// non-numeric string
let b; 					// empty variable
let c = [1, 2, 3, 4];			// array with more than one elements
let d = {};				// empty object

console.log(Math.trunc(a));
console.log(Math.trunc(b));
console.log(Math.trunc(c));
console.log(Math.trunc(d));```

#### Output

```node app
NaN
NaN
NaN
NaN
```

### Javascript trunc(): zero is returned

The following example demonstrates the cases where zero is returned.

```// app.js

let a = null;
let b = "";	// empty string
let c = [];	// empty array

console.log(Math.trunc(a));
console.log(Math.trunc(b));
console.log(Math.trunc(c));
```

#### Output

```node app
0
0
0
```

### Demonstrates the polyfill methods for trunc()

The following example demonstrates the polyfill methods for trunc().

```// app.js

let a = 12.25;
let b = -6.3;
let c = 0.35;
let d = -0.2;
let e = [4.3]; 				// array with single element

function polyfill1(x) {
x = +x;
if (!isFinite(x)) return x;
return (x - x % 1) || (x < 0 ? -0 : x === 0 ? x : 0);
}

function polyfill2(x) {
return x < 0 ? Math.ceil(x) : Math.floor(x);
}

console.log(Math.trunc(a));
console.log(Math.trunc(b));
console.log(Math.trunc(c));
console.log(Math.trunc(d));
console.log(Math.trunc(e));
console.log();

console.log(polyfill1(a));
console.log(polyfill1(b));
console.log(polyfill1(c));
console.log(polyfill1(d));
console.log(polyfill1(e));
console.log();

console.log(polyfill2(a));
console.log(polyfill2(b));
console.log(polyfill2(c));
console.log(polyfill2(d));
console.log(polyfill2(e));
```

#### Output

```node app
12
-6
0
-0
4

12
-6
0
-0
4

12
-6
0
-0
4```

The following example demonstrates the scenario where the trunc method can be used in a simple programming context.

Consider the following problem:

Let’s say a startup has decided to hire new engineers to build their product. But they have a fixed budget. Given the fixed budget and the salary of an engineer, find out the maximum number of engineers the startup can hire.

```// app.js

let budget;
let salary;

const rl = r.createInterface({
input: process.stdin,
output: process.stdout
});

const prompt1 = () => {
return new Promise((resolve, reject) => {
resolve();
});
});
};

const prompt2 = () => {
return new Promise((resolve, reject) => {
resolve();
});
});
};

const main = async () => {
await prompt1();
await prompt2();
rl.close();

console.log(Math.trunc(budget / salary));
}

main();

```

#### Output

```Test Case 1:
->node example5
Budget: 200000
Salary: 26000
7

Test Case 2:
->node example5
Budget: 250000
Salary: 100000
2
```