How to Truncate values in Javascript

In JavaScript, you can truncate a number to a specified number of decimal places using the toFixed() method or you can truncate a number to its integer value using the Math.trunc() method.

Here’s an example of how to use toFixed() method to truncate a number to a specific number of decimal places:

javascript
let num = 3.14159265359;
let truncatedNum = num.toFixed(2);
console.log(truncatedNum); // Output: 3.14

In this example, num is the original number and truncatedNum is the truncated number with 2 decimal places.

Alternatively, you can use the Math.trunc() method to truncate a number to its integer value:

javascript
let num = 3.14159265359;
let truncatedNum = Math.trunc(num);
console.log(truncatedNum); // Output: 3

In this example, num is the original number and truncatedNum is the truncated integer value of num.