对系统的toFixed()方法的修正
来源:互联网 发布:解析xml java 编辑:程序博客网 时间:2024/05/17 07:45
if(typeof(Number.prototype.toFixed)!="function")
{
Number.prototype.toFixed = function(d)
{
var s=this+"";if(!d)d=0;
if(s.indexOf(".")==-1)s+=".";s+=new Array(d+1).join("0");
if (new RegExp("^(-|//+)?(//d+(//.//d{0,"+ (d+1) +"})?)//d*$").test(s))
{
var s="0"+ RegExp.$2, pm=RegExp.$1, a=RegExp.$3.length, b=true;
if (a==d+2){a=s.match(//d/g); if (parseInt(a[a.length-1])>4)
{
for(var i=a.length-2; i>=0; i--) {a[i] = parseInt(a[i])+1;
if(a[i]==10){a[i]=0; b=i!=1;} else break;}
}
s=a.join("").replace(new RegExp("(//d+)(//d{"+d+"})//d$"),"$1.$2");
}if(b)s=s.substr(1);return (pm+s).replace(//.$/, "");} return this+"";
};
}
0.009.toFixed(2)本该返回0.01的结果,可它却返回了一个0.00,这是这个方法的一个BUG,且这个方法对客户端的JS版本要求有点偏高,至少在IE5.0里这个方法用不起来,所以我写了上面的一段修正代码,并且还解决了这个BUG的问题。若是想完全使用这个自定义的方法替代那个有BUG的系统方法的话,只需要去掉最外层的那个 if 判断就可以了。
- 对系统的toFixed()方法的修正
- 对系统的toFixed()方法的修正
- 对系统的toFixed()方法的修正
- JS的toFixed()方法
- js toFixed().方法的延伸
- JS 四舍五入的toFixed()方法
- jquery的toFixed方法的正确使用
- JavaScript中toFixed()方法的用法
- js里 toFixed()方法的坑!
- JS的toFixed方法出错 重写
- JS的toFixed方法出错 重写
- JavaScript Number对象的toFixed() 方法
- wince系统中对nand坏块的修正
- 修正bug的方法
- Query 对ie6的版本检测错误及修正方法
- toFixed方法重写,toFixed四舍五入在数值很小时的精度问题
- JS中toFixed()方法引起的精度丢失的问题解决
- 关于js的toFixed方法的使用注意点
- 你不做伯乐,我就不会做千里马.
- 销售学生管理系统
- LINUX系统中动态链接库的创建与使用
- 排序算法比较程序
- javascript 调用 WebService的方法
- 对系统的toFixed()方法的修正
- 敏捷开发(Agile Development)简介
- Js 调用 WebService心得总结
- gcc参数详解
- JS细节
- 一套完整的makefile样板
- Asp.net 2.0控件开发相关调试(JavaScript调试和自定义控件设计时调试)
- 使用Libxml2解析xml
- JDMK 和遗留信息技术管理