//保留1位小数 function toDecimal1(x) { var f = parseFloat(x); if (isNaN(f)) { return 0; } var f = Math.round(x * 10) / 10; var s = f.toString(); var rs = s.indexOf('.'); if (rs < 0) { rs = s.length; s += '.'; } while (s.length <= rs + 1) { s += '0'; } return s; } //保留2位小数 function toDecimal(x) { var f = parseFloat(x); if (isNaN(f)) { return 0; } var f = Math.round(x * 100) / 100; var s = f.toString(); var rs = s.indexOf('.'); if (rs < 0) { rs = s.length; s += '.'; } while (s.length <= rs + 2) { s += '0'; } return s; }
转载于:https://my.oschina.net/u/3690153/blog/2246508