round - 精确的几位小数

将数字四舍五入到指定的小数位数。

使用 Math.round() 和模板字面量将数字四舍五入为指定的小数位数。 省略第二个参数 decimals ,数字将被四舍五入到一个整数。

const round = (n, decimals = 0) => Number(`${Math.round(`${n}e${decimals}`)}e-${decimals}`);

round(1.005, 2); // 1.01
posted @ 2018-01-18 17:34  大哥哥抓泥鳅  阅读(375)  评论(0编辑  收藏  举报