This article mainly introduces node. in js, You Can format numbers to add thousands of characters. This article provides three implementation methods and provides the instance Code respectively. For more information, see the processing method separated by commas (,).
Regular Expression mode:
The Code is as follows:
"15000000 ". split (""). reverse (). join (""). replace (/(\ d {3})/g, "$1 ,"). split (""). reverse (). join ("");
"115000000". split (""). reverse (). join (""). replace (/(\ d {3 })(? = [^ $])/G, "$1,"). split (""). reverse (). join ("");
Var str = '1970. 100'. replace (/(? = (?! ^ )(? : \ D {3}) + (? : \. | $) (\ D {3} (\. \ d + $ )?) /G, ', $1 ');
Console. log (str)
'123'. replace (/\ B (? = (\ D {3}) + $)/g ,',')
Loop Search Method
The Code is as follows:
Function formatNumber (value ){
Value = value. toString ();
If (value. length <= 3 ){
Return value;
} Else {
Return formatNumber (value. substr (0, value. length-3) + ',' + value. substr (value. length-3 );
}
}
ToLocaleString function:
The Code is as follows:
15000000 .. toLocaleString ();