要保留小数点,可以使用toFixed()方法。该方法将数字转换为字符串,并将其四舍五入到指定的小数位数。例如:

let num = 3.1415926;
let roundedNum = num.toFixed(2); // 保留两位小数
console.log(roundedNum); // 输出 '3.14'

注意,toFixed()返回的是字符串类型,如果需要使用数值类型,可以使用parseFloat()或Number()进行转换。

JavaScript数组保留小数点:使用toFixed()方法