写一个方法保留小数点后面的几位

33 阅读1分钟

"```javascript function roundToDecimal(num, decimalPlaces) { return Number(num.toFixed(decimalPlaces)); }

This `roundToDecimal` function takes two parameters: `num` (the number to be rounded) and `decimalPlaces` (the number of decimal places to keep). It uses the `toFixed` method to round the number to the specified decimal places and then converts the result back to a number using `Number`."