- //法一
- function parseNum(num){
- var list = new String(num).split(‘‘).reverse();
- for(var i = 0; i < list.length; i++){
- if(i % 4 == 3){
- list.splice(i, 0, ‘,‘);
- }
- }
- return list.reverse().join(‘‘);
- }
- console.log(parseNum(10000121213));
- //法二
- function parseNum(num){
- var reg=/(?=(?!\b)(\d{3})+$)/g;
- return String(num).replace(reg, ‘,‘);
- }
- console.log(parseNum(10000121213));
- //法三
- String.prototype.strReverse = function(){
- return this.split(‘‘).reverse().join(‘‘);
- }
- function parseNum(num){
- var str_num = String(num);
- var len = str_num.length;
- var tail = str_num.slice(0, len % 3);
- tail = tail.strReverse();
- var reg=/\d{3}/g;
- var list = str_num.strReverse().match(reg);
- list.push(tail);
- var res = list.join(‘,‘).strReverse();
- return res;
- }
- console.log(parseNum(10000121213));
- //法四
- function parseNum(num){
- var list = String(num).split(‘‘).reverse();
- var temp = [];
- for(var i = 0, len = list.length; i < len; i = i + 3){
- temp.push(list.slice(i, i + 3).join(‘‘));
- }
- return temp.join(‘,‘).split(‘‘).reverse().join(‘‘);
- }
- console.log(parseNum(10000121213));
时间: 2024-10-08 06:21:24