JS 는 어떻게 숫자 유형 을 쉼표 하나 없 는 금전 형식 으로 바 꿉 니까?

5370 단어
3345687687876789123
xxx, xxx, xxx
 
  
<br> <br>window.onload = function(){ <br>// <br>var testFun = function( callback, str ){ <br>var tipElem = document.createElement( 'div' ), <br>startTime, <br>duration = 0; <br>for( var j = 5; j > 0; j-- ){ <br>startTime = +new Date(); <br>for( var i = 10000; i > 0; i-- ){ <br>callback(); <br>} <br>duration = ((+new Date()) - startTime) + duration; <br>} <br>duration = (duration / 5).toFixed(0); <br>tipElem.innerHTML = str + ' :' + duration + ' ms'; <br>document.body.appendChild( tipElem ); <br>}; <br> <br>var str = '3345687687876789123'; <br> <br>var cuter1 = function( str ){// <br>var len = str.length, <br>lastIndex, <br>arr = []; <br>while( len > 0 ){ <br>lastIndex = len; <br>len -= 3; <br>arr.unshift( str.substring(len, lastIndex) ); <br>} <br>return arr.join(','); <br>}; <br> <br> <br>var cuter2 = function( str ){//abcd <br>return str.replace( /\B(?=(?:\d{3})+$)/g, ',' ); <br>}; <br> <br>var cuter3 = function( str ){// <br>return str.replace(/(.*)(\d{3})$/,function(){ <br>if(arguments[1]&& arguments[2]){ <br>return arguments[1].replace(/(.*)(\d{3})$/,arguments.callee)+","+arguments[2]; <br>} else { <br>return arguments[0]; <br>} <br>}); <br>}; <br> <br> <br>var cuter4 = function( str ){//Alucelx <br>return str.split('').reverse().join('').replace(/(\d{3})/g, '$1,').split('').reverse().join(''); <br>}; <br> <br>var cuter5 = function( str ){// <br>var ret = []; <br>while(str){ <br>str = str.replace(/\d{1,3}$/g,function(a){ <br>ret.unshift(a) <br>return "" <br>}); <br>} <br>return ret.join(","); <br>}; <br>var cuter6 = function( str ){// <br>var n = str.length % 3; <br>if(n){ <br>return str.slice(0,n) + str.slice(n).replace(/(\d{3})/g,',$1') <br>}else{ <br>return str.replace(/(\d{3})/g,',$1').slice(1) <br>} <br>}; <br>var cuter7 = function(str){// <br>var ret = "" <br>for(var i = 0, n = str.length, m = n %3 - 1; i < n; i++){ <br>ret += str.charAt(i) <br>if( i % 3 === m ){ <br>ret += "," <br>} <br>} <br>var e = ret.length - 1 <br>return ret.charAt(e) == "," ? ret.slice(0,e) : ret <br>} <br>var cuter8 = function(str){//[[valueOf]] <br>var s2 = [].slice.call(str); <br>for(var i=s2.length-3; i>0;i-=3){ <br>s2.splice(i, 0 ,',' ); <br>} <br>return s2.join("") <br>} <br>var cuter9 = function(str){// <br>var newStr= new Array(str.length+ parseInt(str.length/3)); <br>newStr[newStr.length-1]=str[str.length-1]; <br>var currentIndex=str.length-1; <br>for(var i = newStr.length-1;i >= 0;i--) { <br>if((newStr.length-i)%4==0) <br>{ <br>newStr[i]=","; <br>}else{ <br>newStr[i]=str[currentIndex--]; <br>} <br>} <br>return newStr.join("") <br>} <br>var cuter10 = function(str){//Rekey <br>var len = str.length, str2 = '', max = Math.floor(len / 3); <br>for(var i = 0 ; i < max ; i++){ <br>var s = str.slice(len - 3, len); <br>str = str.substr(0, len - 3); <br>str2 = (',' + s) + str2; <br>len = str.length; <br>} <br>str += str2; <br>return str <br>} <br>// <br>testFun(function(){ <br>cuter1(str); <br>}, ' ' ); <br> <br> <br>testFun(function(){ <br>cuter2(str); <br>}, ' ' ); <br> <br> <br>testFun(function(){ <br>cuter3(str); <br>}, ' ' ); <br> <br> <br>testFun(function(){ <br>cuter4(str); <br>}, ' ' ); <br> <br> <br>testFun(function(){ <br>cuter5(str); <br>}, ' ' ); <br>testFun(function(){ <br>cuter6(str); <br>}, ' ' ); <br>testFun(function(){ <br>cuter7(str); <br>}, ' ' ); <br>testFun(function(){ <br>cuter8(str); <br>}, ' ' ); <br>testFun(function(){ <br>cuter9(str); <br>}, ' ' ); <br>testFun(function(){ <br>cuter10(str); <br>}, ' ' ); <br>} <br>

새로운 테스트 결 과 는:
 
  
:14 ms
:9 ms
:174 ms
:34 ms
:39 ms
:7 ms
:9 ms
:30 ms
:14 ms
:4 ms

좋은 웹페이지 즐겨찾기