js prototype 格式化數(shù)字 By shawl.qiu
更新時(shí)間:2007年04月02日 00:00:00 作者:
說(shuō)明:
最近打算把 Js 練精點(diǎn), 只好暫時(shí)放棄原來(lái)掌握的還行的 VBScript, 全面使用 Jscript/Javascript.
發(fā)現(xiàn) VBs 和 Js 都有些雙方?jīng)]有的功能...
比如 Js 就沒(méi)有 VBs 的 formatNumber, formatN*** 類的函數(shù).
但是 Js 幾乎隨處可用 正則, 這是我的長(zhǎng)處, 這點(diǎn)特吸引我, 不像 VBs 只有 RegExp 使用域可以使用正則.
引用一本書(shū)里的一句話:
The way to really learn a new programming language is to write programs with it.
--JavaScript: The Definitive Guide, 4th Edition
目錄:
1. 內(nèi)容: Number.prototype.formatNumber() 源代碼.
2. 效率測(cè)試
shawl.qiu
2006-10-14
http://blog.csdn.net/btbtd
1. 內(nèi)容: Number.prototype.formatNumber() 源代碼.
linenum
<%
var $num=9876577784321.011
Number.prototype.formatNumber=function(pointPsti){
/*--------------------------------------------------------*\
* Javascript 格式化數(shù)字原型, By shawl.qiu
* 客戶端使用: var $num=9876577784321.011; document.write('<br/>'+$num.formatNumber(3)+'<br/>');
* 服務(wù)端使用: 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. 效率測(cè)試
輸出 10,000 次, 耗時(shí) 2797 毫秒.
輸出 5,000 次, 耗時(shí) 1515 毫秒.
輸出 2,000 次, 耗時(shí) 672 毫秒.
輸出 1,000 次, 耗時(shí) 281 毫秒.
輸出 500 次, 耗時(shí) 140 毫秒.
輸出 100 次, 耗時(shí) 16 毫秒.
最近打算把 Js 練精點(diǎn), 只好暫時(shí)放棄原來(lái)掌握的還行的 VBScript, 全面使用 Jscript/Javascript.
發(fā)現(xiàn) VBs 和 Js 都有些雙方?jīng)]有的功能...
比如 Js 就沒(méi)有 VBs 的 formatNumber, formatN*** 類的函數(shù).
但是 Js 幾乎隨處可用 正則, 這是我的長(zhǎng)處, 這點(diǎn)特吸引我, 不像 VBs 只有 RegExp 使用域可以使用正則.
引用一本書(shū)里的一句話:
The way to really learn a new programming language is to write programs with it.
--JavaScript: The Definitive Guide, 4th Edition
目錄:
1. 內(nèi)容: Number.prototype.formatNumber() 源代碼.
2. 效率測(cè)試
shawl.qiu
2006-10-14
http://blog.csdn.net/btbtd
1. 內(nèi)容: Number.prototype.formatNumber() 源代碼.
linenum
復(fù)制代碼 代碼如下:
<%
var $num=9876577784321.011
Number.prototype.formatNumber=function(pointPsti){
/*--------------------------------------------------------*\
* Javascript 格式化數(shù)字原型, By shawl.qiu
* 客戶端使用: var $num=9876577784321.011; document.write('<br/>'+$num.formatNumber(3)+'<br/>');
* 服務(wù)端使用: 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. 效率測(cè)試
輸出 10,000 次, 耗時(shí) 2797 毫秒.
輸出 5,000 次, 耗時(shí) 1515 毫秒.
輸出 2,000 次, 耗時(shí) 672 毫秒.
輸出 1,000 次, 耗時(shí) 281 毫秒.
輸出 500 次, 耗時(shí) 140 毫秒.
輸出 100 次, 耗時(shí) 16 毫秒.
您可能感興趣的文章:
- node.js中格式化數(shù)字增加千位符的幾種方法
- JS格式化數(shù)字金額用逗號(hào)隔開(kāi)保留兩位小數(shù)
- JS格式化數(shù)字保留兩位小數(shù)點(diǎn)示例代碼
- js為數(shù)字添加逗號(hào)并格式化數(shù)字的代碼
- JavaScript格式化數(shù)字的函數(shù)代碼
- js保留兩位小數(shù)方法總結(jié)
- javascript實(shí)現(xiàn)保留兩位小數(shù)的多種方法
- js浮點(diǎn)數(shù)保留兩位小數(shù)點(diǎn)示例代碼(四舍五入)
- JavaScript 格式化數(shù)字、金額、千分位、保留幾位小數(shù)、舍入舍去
相關(guān)文章
JavaScript數(shù)據(jù)存儲(chǔ) Cookie篇
這篇文章主要為大家介紹了JavaScript數(shù)據(jù)存儲(chǔ) Cookie篇,感興趣的朋友可以參考一下2016-07-07
JavaScript實(shí)現(xiàn)的多個(gè)圖片廣告交替顯示效果代碼
這篇文章主要介紹了JavaScript實(shí)現(xiàn)的多個(gè)圖片廣告交替顯示效果代碼,涉及javascript數(shù)組遍歷及頁(yè)面元素動(dòng)態(tài)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09

