toFixed() 方法返回一个表示 numObj 的字符串,但不使用指数计数法,并且小数点后有精确到 digits 位的数字。如果需要截断,则将数字四舍五入;如果小数位数不足,则小数部分用零填充,以使其具有指定长度。
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head><body><script>const num = 10.985console.log(num.toFixed())//()就是四舍五入console.log(num.toFixed(1))//11.0console.log(num.toFixed(2))//10.98</script>
</body></html>
