js prototype 格式化数字 By shawl.qiu

78 篇文章 0 订阅
31 篇文章 0 订阅

js prototype 格式化数字 By shawl.qiu


说明:
最近打算把 Js 练精点, 只好暂时放弃原来掌握的还行的 VBScript, 全面使用 Jscript/Javascript. 
发现 VBs 和 Js 都有些双方没有的功能...
比如 Js 就没有 VBs 的 formatNumber, formatN*** 类的函数. 
但是 Js 几乎随处可用 正则, 这是我的长处, 这点特吸引我, 不像 VBs 只有 RegExp 使用域可以使用正则.

引用一本书里的一句话:
The way to really learn a new programming language is to write programs with it.
--JavaScript: The Definitive Guide, 4th Edition

目录:
1. 内容: Number.prototype.formatNumber() 源代码.
2. 效率测试

shawl.qiu 
2006-10-14
http://blog.csdn.net/btbtd

1. 内容: Number.prototype.formatNumber() 源代码.
  1. linenum
  2. <%
  3.     var $num=9876577784321.011
  4.         Number.prototype.formatNumber=function(pointPsti){
  5.         /*--------------------------------------------------------*/  
  6.          *    Javascript 格式化数字原型, By shawl.qiu
  7.          *    客户端使用: var $num=9876577784321.011; document.write('<br/>'+$num.formatNumber(3)+'<br/>');
  8.          *    服务端使用: var $num=9876577784321.011; Response.Write($num.formatNumber(3)); 
  9.         /*--------------------------------------------------------*/
  10.             if(this=='')return false;
  11.             if(typeof(pointPsti)=='undefined'){
  12.                 var pointPsti=3;
  13.             } else { if(isNaN(pointPsti)){pointPsti=3}; }
  14.             var num=this+'', numDc='', temp='';
  15.             if(num.indexOf('.')>-1){ ptPs=num.indexOf('.'); numDc=num.substr(ptPs); num=num.substr(0,ptPs); }
  16.             for(var i=num.length-1; i>=0;temp+=num.substr(i,1), i--);
  17.             var re=new RegExp('(.{'+pointPsti+'})','g');
  18.                 temp=temp.replace(re,'$1,'); num='';                
  19.             for(var i=temp.length-1; i>=0; num+=temp.substr(i,1), i--);
  20.                 num=num.replace(/^/,|/,$/,'')+numDc;
  21.             return num; // shawl.qiu script
  22.         }
  23.             Response.Write($num.formatNumber(3)+'<br/>');
  24. %>

2. 效率测试
输出 10,000 次, 耗时 2797 毫秒. 
输出 5,000 次, 耗时 1515 毫秒. 
输出 2,000 次, 耗时 672 毫秒. 
输出 1,000 次, 耗时 281 毫秒. 
输出 500 次, 耗时 140 毫秒. 
输出 100 次, 耗时 16 毫秒. 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值