During the currency conversion in the project, you often need to enable automatic formatting of the entered number and automatic thousands of separators. on the Internet, I am not very satisfied with the code implemented by other netizens, so I studied it and shared it with you. Recently, I saw an interview (written test) question about how to use JavaScript to implement thousands of separators of numbers. so I wrote a method to implement it using "Regular Expression + replace:
The code is as follows:
Var thousandBitSeparator = function (numStr ){
Var B =/([-+]? \ D {3 })(? = \ D)/g;
Return numStr. replace (B, function ($0, $1 ){
Return $1 + ',';
});
}
Supports positive and negative matching and decimal point differentiation. if there is an error, we hope to point it out:-D
Attach the implementation method of another user
The code is as follows: