使用Math.round():letnum=0.1234;letres=Math.round/100;console.log;或者为了确保正确地处理1.005之类的数据,请使用Number.EPSILON:Number.EPSILON属性表示1与Number可表示的大于1的最小的浮点数之间的差值。

let num = 0.1234;let res = Math.round(num * 100) / 100;console.log(res);
Number.EPSILON 属性表示 1 与Number可表示的大于 1 的最小的浮点数之间的差值。
let num = 1.005;let res = Math.round(num * 100) / 100;let resWithEPSILON = Math.round((numNumber.EPSILON) * 100) / 100;console.log(res);//1console.log(resWithEPSILON); //1.01
