游子日月长

笑渐不闻声渐悄,多情却被无情恼!

导航

用js来实现银行家算法

 

Number.prototype.round = function (len) {
var old = this;
var a1 = Math.pow(10, len) * old;
a1 = Math.round(a1);
var oldstr = old.toString()
var start = oldstr.indexOf(".");
if (start > 0 && oldstr.split(".")[1].length == len + 1) {
if (oldstr.substr(start + len + 1, 1) == 5) {
var flagval = oldstr.substr(start + len, 1) - 0;
if (flagval % 2 == 0) {
a1 = a1 - 1;
}
}
}
var b1 = a1 / Math.pow(10, len);
return b1;
}
Number.prototype.oldtoFixed = Number.prototype.toFixed;
Number.prototype.toFixed = function (len) {
var old = this;
var oldstr = old.toString()
var start = oldstr.indexOf(".");
if (len == 2 && start > 0 && oldstr.split(".")[1].length == 3) {
return this.round(len);
}
else {
return this.oldtoFixed(len);
}
}

posted on 2016-11-11 10:48  游子日月长  阅读(630)  评论(0编辑  收藏  举报