Note:
Recently, I plan to refine my Javascript code, so I have to temporarily discard the original good VBScript and fully use Jscript/Javascript.
We found that both VBs and Js have some functions that are not available on both sides...
For example, Js does not have the formatNumber and formatN *** class functions of VBs.
However, regular expressions are available almost everywhere in Js. This is my strength and attracts me. Unlike VBs, only RegExp can use regular expressions in the domain.
Reference a sentence in a book:
The way to really learn a new programming language is to write programs with it.
-- JavaScript: The Definitive Guide, 4th Edition
Directory:
1. Content: Number. prototype. formatNumber () source code.
2. Efficiency Test
Shawl. qiu
2006-10-14
Http://blog.csdn.net/btbtd
1. Content: Number. prototype. formatNumber () source code.
LinenumCopy codeThe Code is as follows: <%
Var $ num = 9876577784321.011
Number. prototype. formatNumber = function (pointPsti ){
/*--------------------------------------------------------*\
* Javascript format the digit prototype By shawl. qiu
* Client usage: var $ num = 9876577784321.011; document. write ('<br/>' + $ num. formatNumber (3) + '<br/> ');
* Server use: 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. Efficiency Test
It takes 10,000 milliseconds to output 2797 times.
It takes 5,000 milliseconds to output 1515 times.
It takes 2,000 milliseconds to output 672 times.
It takes 1,000 milliseconds to output 281 times.
It takes 500 milliseconds to output 140 times.
It takes 16 milliseconds to output 100 times.