1 var myf='6.202555'; 2 3 myf=Number(myf).toFixed(2);//使用方法 4 5 6 Number.prototype.toFixed = function(scale) { 7 var s = this + ""; 8 if (!scale) scale = 0; 9 if (s.indexOf(".") == -1) s += "."; 10 s += new Array(scale + 1).join("0"); 11 if (new RegExp("^(-|\\+)?(\\d+(\\.\\d{0," + (scale + 1) + "})?)\\d*$").test(s)) { 12 13 var s = "0" + RegExp.$2, pm = RegExp.$1, a = RegExp.$3.length, b = true; 14 if (a == scale + 2) { 15 a = s.match(/\d/g); 16 if (parseInt(a[a.length - 1]) > 4) { 17 for (var i = a.length - 2; i >= 0; i--) { 18 a[i] = parseInt(a[i]) + 1; 19 if (a[i] == 10) { 20 a[i] = 0; 21 b = i != 1; 22 }else { 23 break; 24 } 25 } 26 } 27 28 s = a.join("").replace(new RegExp("(\\d+)(\\d{" + scale + "})\\d$"), "$1.$2"); 29 } 30 if (b){ 31 s = s.substr(1); 32 } 33 return (pm + s).replace(/\.$/, ""); 34 } 35 return this + ""; 36 }