Number.prototype.toFixed = function(s) {
if (this == 0) {
return 0;
}
if (this == ~~this) {
changenum = this;
} else {
var number = Math.abs(-this) + 1 / Math.pow(10, Math.abs(-this).toString().length - 1); //往最后补一位
changenum = Math.round(number * Math.pow(10, s)) / Math.pow(10, s);
}
var index = changenum.toString().indexOf(".");
if (index < 0 && s > 0) {
changenum = changenum + ".";
for (var ii = 0; ii < s; ii++) {
changenum = changenum + "0";
}
} else {
index = changenum.toString().length - index;
for (var i = 0; i < (s - index) + 1; i++) {
changenum = changenum + "0";
}
}
if (this > 0)
return changenum;
else
return -changenum;
}
alert(parseFloat(“157.295”).toFixed(2))