四舍五入 : spa
var nubm = 6.6666666; var str = nubm.toFixed(2); nubm = +str ; console.log(nubm);
不四舍五入
如下处理结果不会四舍五入:code
第一种blog
var numb = 5555.555555 n =numb.toString(10) n =n.slice(0, n.indexOf('.') + 3); console.log(n)
第二种,先把小数边整数:字符串
Math.floor(15.7784514000 * 100) / 100 // 输出结果为 15.77
第三种,看成字符串,使用正则匹配:console
Number(15.7784514000.toString().match(/^\d+(?:\.\d{0,2})?/)) // 输出结果为 15.77,不能用于整数如 10 必须写为10.0000
注意:若是是负数,请先转换为正数再计算,最后转回负数class