调用方法示例:$('#count').html(toDecimal2(shuliang_count));
function toDecimal2(x) { //保留小数 if (x != '' && !isNaN(x)) { var f = parseFloat(x); if (isNaN(f)) { return false; } 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; } else { return ''; //非数字,返回空 } }