js prototype 格式化數(shù)字 By shawl.qiu
更新時間:2007年04月02日 00:00:00 作者:
說明:
最近打算把 Js 練精點, 只好暫時放棄原來掌握的還行的 VBScript, 全面使用 Jscript/Javascript.
發(fā)現(xiàn) VBs 和 Js 都有些雙方?jīng)]有的功能...
比如 Js 就沒有 VBs 的 formatNumber, formatN*** 類的函數(shù).
但是 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. 內(nèi)容: Number.prototype.formatNumber() 源代碼.
2. 效率測試
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. 效率測試
輸出 10,000 次, 耗時 2797 毫秒.
輸出 5,000 次, 耗時 1515 毫秒.
輸出 2,000 次, 耗時 672 毫秒.
輸出 1,000 次, 耗時 281 毫秒.
輸出 500 次, 耗時 140 毫秒.
輸出 100 次, 耗時 16 毫秒.
最近打算把 Js 練精點, 只好暫時放棄原來掌握的還行的 VBScript, 全面使用 Jscript/Javascript.
發(fā)現(xiàn) VBs 和 Js 都有些雙方?jīng)]有的功能...
比如 Js 就沒有 VBs 的 formatNumber, formatN*** 類的函數(shù).
但是 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. 內(nèi)容: Number.prototype.formatNumber() 源代碼.
2. 效率測試
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. 效率測試
輸出 10,000 次, 耗時 2797 毫秒.
輸出 5,000 次, 耗時 1515 毫秒.
輸出 2,000 次, 耗時 672 毫秒.
輸出 1,000 次, 耗時 281 毫秒.
輸出 500 次, 耗時 140 毫秒.
輸出 100 次, 耗時 16 毫秒.
相關(guān)文章
微信小程序?qū)崿F(xiàn)幸運大轉(zhuǎn)盤功能的示例代碼
這篇文章主要介紹了如何在微信小程序中實現(xiàn)幸運大轉(zhuǎn)盤功能,參與用戶點擊抽獎便可抽取輪盤的獎品,感興趣的小伙伴可以跟隨小編一起動手試一試2022-03-03
JS中的oninput和onchange事件的區(qū)別及如何正確使用
在JavaScript中,oninput和onchange事件是用于處理用戶輸入的常見事件,本文將介紹oninput和onchange事件的區(qū)別,以及如何在實際開發(fā)中正確使用它們,感興趣的朋友跟隨小編一起看看吧2023-10-10
showModalDialog 和 showModelessDialog
showModalDialog 和 showModelessDialog...2007-01-01
如何基于filter實現(xiàn)網(wǎng)站整體變灰功能
這篇文章主要介紹了如何基于filter實現(xiàn)網(wǎng)站整體變灰功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-04-04

