js prototype 格式化數(shù)字 By shawl.qiu
更新時間:2007年04月02日 00:00:00 作者:
說明:
最近打算把 Js 練精點(diǎn), 只好暫時放棄原來掌握的還行的 VBScript, 全面使用 Jscript/Javascript.
發(fā)現(xiàn) VBs 和 Js 都有些雙方?jīng)]有的功能...
比如 Js 就沒有 VBs 的 formatNumber, formatN*** 類的函數(shù).
但是 Js 幾乎隨處可用 正則, 這是我的長處, 這點(diǎn)特吸引我, 不像 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 練精點(diǎn), 只好暫時放棄原來掌握的還行的 VBScript, 全面使用 Jscript/Javascript.
發(fā)現(xiàn) VBs 和 Js 都有些雙方?jīng)]有的功能...
比如 Js 就沒有 VBs 的 formatNumber, formatN*** 類的函數(shù).
但是 Js 幾乎隨處可用 正則, 這是我的長處, 這點(diǎn)特吸引我, 不像 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
復(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. 效率測試
輸出 10,000 次, 耗時 2797 毫秒.
輸出 5,000 次, 耗時 1515 毫秒.
輸出 2,000 次, 耗時 672 毫秒.
輸出 1,000 次, 耗時 281 毫秒.
輸出 500 次, 耗時 140 毫秒.
輸出 100 次, 耗時 16 毫秒.
您可能感興趣的文章:
- node.js中格式化數(shù)字增加千位符的幾種方法
- JS格式化數(shù)字金額用逗號隔開保留兩位小數(shù)
- JS格式化數(shù)字保留兩位小數(shù)點(diǎn)示例代碼
- js為數(shù)字添加逗號并格式化數(shù)字的代碼
- JavaScript格式化數(shù)字的函數(shù)代碼
- js保留兩位小數(shù)方法總結(jié)
- javascript實(shí)現(xiàn)保留兩位小數(shù)的多種方法
- js浮點(diǎn)數(shù)保留兩位小數(shù)點(diǎn)示例代碼(四舍五入)
- JavaScript 格式化數(shù)字、金額、千分位、保留幾位小數(shù)、舍入舍去
相關(guān)文章
微信小程序?qū)崿F(xiàn)幸運(yùn)大轉(zhuǎn)盤功能的示例代碼
這篇文章主要介紹了如何在微信小程序中實(shí)現(xiàn)幸運(yùn)大轉(zhuǎn)盤功能,參與用戶點(diǎn)擊抽獎便可抽取輪盤的獎品,感興趣的小伙伴可以跟隨小編一起動手試一試2022-03-03JS中的oninput和onchange事件的區(qū)別及如何正確使用
在JavaScript中,oninput和onchange事件是用于處理用戶輸入的常見事件,本文將介紹oninput和onchange事件的區(qū)別,以及如何在實(shí)際開發(fā)中正確使用它們,感興趣的朋友跟隨小編一起看看吧2023-10-10showModalDialog 和 showModelessDialog
showModalDialog 和 showModelessDialog...2007-01-01document節(jié)點(diǎn)對象的獲取方式示例介紹
如果要操作一個元素,首先要獲取到它,下面為大家介紹下document節(jié)點(diǎn)對象的獲取方式,感興趣的朋友不要錯過2013-12-12如何基于filter實(shí)現(xiàn)網(wǎng)站整體變灰功能
這篇文章主要介紹了如何基于filter實(shí)現(xiàn)網(wǎng)站整體變灰功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04Javascript實(shí)現(xiàn)簡單二級下拉菜單實(shí)例
這篇文章主要介紹Javascript實(shí)現(xiàn)二級下拉菜單的具體過程,需要的朋友可以參考下2014-06-06