js prototype 格式化数字 By shawl.qiu_Javascript教程-查字典教程网
js prototype 格式化数字 By shawl.qiu
js prototype 格式化数字 By shawl.qiu
发布时间:2016-12-30 来源:查字典编辑
摘要:说明:最近打算把Js练精点,只好暂时放弃原来掌握的还行的VBScript,全面使用Jscript/Javascript.发现VBs和Js都有...

说明:

最近打算把Js练精点,只好暂时放弃原来掌握的还行的VBScript,全面使用Jscript/Javascript.

发现VBs和Js都有些双方没有的功能...

比如Js就没有VBs的formatNumber,formatN***类的函数.

但是Js几乎随处可用正则,这是我的长处,这点特吸引我,不像VBs只有RegExp使用域可以使用正则.

引用一本书里的一句话:

Thewaytoreallylearnanewprogramminglanguageistowriteprogramswithit.

--JavaScript:TheDefinitiveGuide,4thEdition

目录:

1.内容:Number.prototype.formatNumber()源代码.

2.效率测试

shawl.qiu

2006-10-14

http://blog.csdn.net/btbtd

1.内容:Number.prototype.formatNumber()源代码.

linenum

复制代码 代码如下:

<%

var$num=9876577784321.011

Number.prototype.formatNumber=function(pointPsti){

/*--------------------------------------------------------*

*Javascript格式化数字原型,Byshawl.qiu

*客户端使用:var$num=9876577784321.011;document.write('<br/>'+$num.formatNumber(3)+'<br/>');

*服务端使用:var$num=9876577784321.011;Response.Write($num.formatNumber(3));

*--------------------------------------------------------*/

if(this=='')returnfalse;

if(typeof(pointPsti)=='undefined'){

varpointPsti=3;

}else{if(isNaN(pointPsti)){pointPsti=3};}

varnum=this+'',numDc='',temp='';

if(num.indexOf('.')>-1){ptPs=num.indexOf('.');numDc=num.substr(ptPs);num=num.substr(0,ptPs);}

for(vari=num.length-1;i>=0;temp+=num.substr(i,1),i--);

varre=newRegExp('(.{'+pointPsti+'})','g');

temp=temp.replace(re,'$1,');num='';

for(vari=temp.length-1;i>=0;num+=temp.substr(i,1),i--);

num=num.replace(/^,|,$/,'')+numDc;

returnnum;//shawl.qiuscript

}

Response.Write($num.formatNumber(3)+'<br/>');

%>

2.效率测试

输出10,000次,耗时2797毫秒.

输出5,000次,耗时1515毫秒.

输出2,000次,耗时672毫秒.

输出1,000次,耗时281毫秒.

输出500次,耗时140毫秒.

输出100次,耗时16毫秒.

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新Javascript教程学习
    热门Javascript教程学习
    编程开发子分类