将数字四舍五入到指定的小数位数。
使用 Math.round()
和模板字面量将数字四舍五入为指定的小数位数。 省略第二个参数 decimals
,数字将被四舍五入到一个整数。
const round = (n, decimals = 0) => Number(`${Math.round(`${n}e${decimals}`)}e-${decimals}`);
查看示例
round(1.005, 2); // 1.01
将数字四舍五入到指定的小数位数。
使用 Math.round()
和模板字面量将数字四舍五入为指定的小数位数。 省略第二个参数 decimals
,数字将被四舍五入到一个整数。
const round = (n, decimals = 0) => Number(`${Math.round(`${n}e${decimals}`)}e-${decimals}`);
查看示例
round(1.005, 2); // 1.01
转载于:https://www.cnblogs.com/bali123/p/8311431.html