NOTE开发笔记与题库FETCHED

JS 四舍五入

四舍五入可以使用 toFixed() 方法,toFixed() 方法可把 Number 四舍五入为指定小数位数的数字。 代码如下: toFixed() 方法 [mycode3 type='js'] var num =2.446242342; num = num.toFixed(2); // 输出结果为 2.45 [/mycode3]尝试一下 » 另外像 round()、floor()、ceil() 等都不能真正..

更新于 2026/7/16

教程详情页默认 noindex。

四舍五入可以使用 toFixed() 方法,toFixed() 方法可把 Number 四舍五入为指定小数位数的数字。

代码如下:

toFixed() 方法

var num =2.446242342; num = num.toFixed(2); // 输出结果为 2.45

尝试一下 »

另外像 round()、floor()、ceil() 等都不能真正的四舍五入,有精度问题。

round() 可以通过以下方式来确保精度是正确的:

round() 方法

var num =2.446242342; num = Math.round((num + Number.EPSILON) * 100) / 100; // 输出结果为 2.45

尝试一下 »

参考文章