美文网首页
不四舍五入的保留 n 位小数

不四舍五入的保留 n 位小数

作者: zhao_ran | 来源:发表于2021-09-27 09:49 被阅读0次
const toFixed = (n, fixed) => ~~(Math.pow(10, fixed) * n) / Math.pow(10, fixed);
// 事例
toFixed(25.198726354, 1);       // 25.1
toFixed(25.198726354, 2);       // 25.19
toFixed(25.198726354, 3);       // 25.198
toFixed(25.198726354, 4);       // 25.1987
toFixed(25.198726354, 5);       // 25.19872
toFixed(25.198726354, 6);       // 25.198726

相关文章

网友评论

      本文标题:不四舍五入的保留 n 位小数

      本文链接:https://www.haomeiwen.com/subject/catsgltx.html