1. toFixed() method: This method converts a number to a string and retains the specified number of decimal places.
let num = 3.1415926;
let result = num.toFixed(2);
// "3.14"
2. Math.round() method: This method rounds a number to a specified number of decimal places.
2. Math.round() 方法:该方法将数字四舍五入到指定的小数位数。
3. parseFloat() and regular expressions: This methodconverts a string to a number and retains the specified number of decimal places.
let num = "3.1415926";
let result = parseFloat(num).toFixed(2);
// "3.14"
4. Number() and regular expressions: This method converts a string into a number and retains the specified number of decimal places.
let num = "3.1415926";
let result = Number(num.match(/^\d+(?:\.\d{0,2})?/));
// 3.14
Readers can choose the methods provided above according to their needs. Your support is the driving force for my creation.