Insert a comma every three digits from right to left.
Source: Internet
Author: User
Window. onloadfunction () {// the whole test is provided by Alucelx vartestFunfunction (callback, str) {vartipElemdocument. createElement (& amp; #39; div & amp; #39;), startTime, duration0; fo... window. onload = function (){
// The entire test is provided by Alucelx
Var testFun = function (callback, str ){
Var tipElem = document. createElement ('div '),
StartTime,
Duration = 0;
For (var j = 5; j> 0; j --){
StartTime = + new Date ();
For (var I = 10000; I> 0; I --){
Callback ();
}
Duration = (+ new Date ()-startTime) + duration;
}
Duration = (duration/5). toFixed (0 );
TipElem. innerHTML = str + 'total time consumed: '+ duration + 'ms ';
Document. body. appendChild (tipElem );
};
Var str = '20140901 ';
Var cuter1 = function (str) {// with a knife
Var len = str. length,
LastIndex,
Arr = [];
While (len> 0 ){
LastIndex = len;
Len-= 3;
Arr. unshift (str. substring (len, lastIndex ));
}
Return arr. join (',');
};
Var cuter2 = function (str) {// abcd
Return str. replace (/\ B (? = (? : \ D {3}) + $)/g ,',');
};
Var cuter3 = function (str) {// former uncle
Return str. replace (/(. *) (\ d {3}) $/, function (){
If (arguments [1] & arguments [2]) {
Return arguments [1]. replace (/(. *) (\ d {3}) $/, arguments. callee) + "," + arguments [2];
} Else {
Return arguments [0];
}
});
};
Var cuter4 = function (str) {// Alucelx
Return str. split (''). reverse (). join (''). replace (/(\ d {3})/g, '$1 ,'). split (''). reverse (). join ('');
};
Var cuter5 = function (str) {// situ zhengmei
Var ret = [];
While (str ){
Str = str. replace (/\ d {1, 3} $/g, function (){
Ret. unshift ()
Return ""
});
}
Return ret. join (",");
};
Var cuter6 = function (str) {// situ zhengmei
Var n = str. length % 3;
If (n ){
Return str. slice (0, n) + str. slice (n). replace (/(\ d {3})/g, ', $1 ')
} Else {
Return str. replace (/(\ d {3})/g, ', $ 1'). slice (1)
}
};
Var cuter7 = function (str) {// situ zhengmei
Var ret = ""
For (var I = 0, n = str. length; I <n; I ++ ){
Ret + = str. charAt (I)
If (I % 3 = 0 ){
Ret + = ","
}
}
Return ret
}
Var cuter8 = function (str) {// [[valueOf]
Var s2 = []. slice. call (str );
For (var I = s2.length-3; I> 0; I-= 3 ){
S2.splice (I, 0 ,',');
}
Return s2.join ("")
}
Var cuter9 = function (str) {// heard
Var newStr = new Array (str. length + parseInt (str. length/3 ));
NewStr [newStr. length-1] = str [str. length-1];
Var currentIndex = str. length-1;
For (var I = newStr. length-1; I> = 0; I --){
If (newStr. length-I) % 4 = 0)
{
NewStr [I] = ",";
} Else {
NewStr [I] = str [currentIndex --];
}
}
Return newStr. join ("")
}
Var cuter10 = function (str) {// Rekey
Var len = str. length, str2 = '', max = Math. floor (len/3 );
For (var I = 0; I <max; I ++ ){
Var s = str. slice (len-3, len );
Str = str. substr (0, len-3 );
Str2 = (',' + s) + str2;
Len = str. length;
}
Str + = str2;
Return str
}
// The following is a performance test
TestFun (function (){
Cuter1 (str );
}, 'Method 1 ');
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.