In JavaScript, you can use the toFixed() method to round a number to a certain number of decimal places. The toFixed() method takes an integer argument, which is the number of decimal places to round to.

Example:

let number = 3.14159;
let rounded = number.toFixed(2);
console.log(rounded); // Output: "3.14"

Alternatively, you can use the Math.round() method to round a number to the nearest integer, and then divide the result by a power of 10 to get the desired number of decimal places.

Example:

let number = 3.14159;
let rounded = Math.round(number * 100) / 100;
console.log(rounded); // Output: "3.14"

You can also use a combination of Math.round() and toPrecision() method:

let num = 3.14159;
let rounded = Math.round(num*100)/100;
console.log(rounded.toPrecision(2)); // Output: "3.14"

Also Read:

Categorized in: