Js prototype format the number By shawl. qiu

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.