当前位置: 首页 > 图文教程 > 网络编程 > Javascript > js prototype 格式化数字 By shawl.qiu

Javascript
javascript 汉字与拼音转换
JavaScript使用cookie
大平洋汽车网左侧菜单
使用JS操作页面表格,元素的一些技巧
分享我学习js的过程 作者aircy javascript学习教程
张孝祥JavaScript学习阶段性总结(2)--(X)HTML学习
用js来生成随机彩票号码清单
发一个数据过滤的代码,很简单,有用的着的拿去
如何在标题栏显示框架内页面的标题
js滚动条多种样式,推荐
再次更新!MSClass (Class Of Marquee Scroll通用不间断滚动JS封装类 Ver 1.6)
符合web标准的连续滚动图像的js代码
使用iframe作为日历的载体,不再被select和flash等控件挡住的日期输入框
通过脚本控制指定内容不能被选择
模拟弹出窗口效果,关闭层之前,不能选择后面的页内容
另类弹出窗口,跳过所有拦截工具
使用prototype.js进行异步操作
JS模拟多线程
动态增加/删除文件域
[分享]一个非常漂亮的进度滚动条

Javascript 中的 js prototype 格式化数字 By shawl.qiu


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 91 ::
收藏到网摘: n/a

说明:
最近打算把 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 毫秒.