js toFixed向下取整的简单方法

javaScript的toFixed默认是四舍五入的方式保留小数。
网上没找到简单的方法。
这里用一条语句搞定它:

比如取3位小数

let a = 1.23456789;
// 四舍五入
let b = a.toFixed(3);
b: 1.235

// 向下取整:
let n = a.toFixed(3 + 1)
b = n.splice(0, n.length - 1);
b: 1.234

评论

暂无评论