The number that will represent the amount, in the thousands of quartiles format, should be a more common requirement. The main thing is to take into account the decimals, so let’s just look at the code.
1. How to write the code?
<style>
body {
background: # 090822;
}
.container { float: left; margin-right: 30px; } .money { font-size: 60px; font-family: 'myCoolFont'; /* Custom font name */ color: #07c6ff; } </style> </head> <body> <div class="formate-before container"> <div class="money1 money">520520850</div> <div class="money2 money">13141314.1314</div> <div class="money3 money">- 996996996.</div> <div class="money4 money">95279.5279</div> </div> <div class="formate-after container"> <div class="money1 money"></div> <div class="money2 money"></div> <div class="money3 money"></div> <div class="money4 money"></div> </div> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script> var number1 = formateNumbers(520520850), number2 = formateNumbers(13141314.1314), number3 = formateNumbers(- 9996996996.), number4 = formateNumbers(95279.5279); $('.formate-after .money1').text(number1); $('.formate-after .money2').text(number2); $('.formate-after .money3').text(number3); $('.formate-after .money4').text(number4); / * ** @description: used to place numeric amounts in thousands of decimal format* @param {Number} num Specifies the Number to process* @param {String} mark Delimited characters* @return:{String} The amount in thousandths* / function formateNumbers(number, mark) { var separator = mark || ', '; // Divide numbers into arrays based on the decimal point '.' var arrNumber = number && number.toString().split('. '); if (arrNumber && arrNumber.length) { // Add a separator to the number before the decimal point arrNumber[0] = arrNumber[0].replace(/\B(? =(\d{3})+(? ! \d))/g, separator); } // Returns an array as a string return arrNumber && arrNumber.join('. '); } </script> Copy the code
You can customize the characters used to separate the characters. The default is comma.
2. Sample code download
Can copy the code to run to check the use effect, also can to making: https://github.com/Jackyyans/code123 download, more examples will be continuously updated, welcome the attention.