js prototype 格式化数字 By shawl.qiu
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了js prototype 格式化数字 By shawl.qiu,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2123字,纯文字阅读大概需要4分钟。
内容图文
![js prototype 格式化数字 By shawl.qiu](/upload/InfoBanner/zyjiaocheng/376/af25a250caff45d6a954ccffe1f22fb0.jpg)
最近打算把 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() 源代码.
linenum
代码如下:
<%
var $num=9876577784321.011
Number.prototype.formatNumber=function(pointPsti){
/*--------------------------------------------------------*\
* Javascript 格式化数字原型, By shawl.qiu
* 客户端使用: var $num=9876577784321.011; document.write('<br/>'+$num.formatNumber(3)+'<br/>');
* 服务端使用: var $num=9876577784321.011; Response.Write($num.formatNumber(3));
\*--------------------------------------------------------*/
if(this=='')return false;
if(typeof(pointPsti)=='undefined'){
var pointPsti=3;
} else { if(isNaN(pointPsti)){pointPsti=3}; }
var num=this+'', numDc='', temp='';
if(num.indexOf('.')>-1){ ptPs=num.indexOf('.'); numDc=num.substr(ptPs); num=num.substr(0,ptPs); }
for(var i=num.length-1; i>=0;temp+=num.substr(i,1), i--);
var re=new RegExp('(.{'+pointPsti+'})','g');
temp=temp.replace(re,'$1,'); num='';
for(var i=temp.length-1; i>=0; num+=temp.substr(i,1), i--);
num=num.replace(/^\,|\,$/,'')+numDc;
return num; // shawl.qiu script
}
Response.Write($num.formatNumber(3)+'<br/>');
%>
2. 效率测试
输出 10,000 次, 耗时 2797 毫秒.
输出 5,000 次, 耗时 1515 毫秒.
输出 2,000 次, 耗时 672 毫秒.
输出 1,000 次, 耗时 281 毫秒.
输出 500 次, 耗时 140 毫秒.
输出 100 次, 耗时 16 毫秒.
内容总结
以上是互联网集市为您收集整理的js prototype 格式化数字 By shawl.qiu全部内容,希望文章能够帮你解决js prototype 格式化数字 By shawl.qiu所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。