千位分隔符实现

·  阅读 772

千位分隔符实现

 千位分隔符实现很常见,例如-1,233,552,214,233.214;


function getType(num) {
  return Object.prototype.toString.call(num);
}

function thoundBitSeparator(num) {
  'use strict';
  let type = getType(num);
  if(type !== '[object Number]') {
    throw new TypeError(`${type} is not number`);
  }
  let regExp = /^([+-])?(\d+)(?=\d{3})(\.\d+)?/,
      strNum = num + '',
      $1, $s;
  while(regExp.test(strNum)) {
    $1 = RegExp.$1;
    $2 = RegExp.$2;
    strNum = $1 + $2 + ',' + strNum.replace($1 + $2, '');
  }

  return strNum;
}

复制代码
thoundBitSeparator(-12514752232.21);   //-12,514,752,232.21
thoundBitSeparator(10);              //10

复制代码
function thoundBitSeparator(num) {
  'use strict';
  let regExp = /(?=(?!^)((\d{3})+)$)/g;
  return num && num.toString().replace(/^\d+/, (num) => {
    return num.replace(regExp, ',');
  });
}

复制代码
function thoundBitSeparator(num) {
  'use strict';
  let type = getType(num);
  if(type !== '[object Number]') {
    throw new TypeError(`${type} is not number`);
  }

  let strNum = String(num),
      startIndex = /^[+-]/.test(strNum) ? 1 : 0,
      dotIndex = strNum.indexOf('.'),
      endIndex = dotIndex !== -1 ? dotIndex : strNum.length,
      thoundBit = [],
      index = endIndex,
      lastIndex = -1;

    while(index > startIndex) {
       index -= 3;
       lastIndex = thoundBit.length !== 0 
         ? strNum.indexOf(thoundBit[0]) 
         : strNum.length + 1;
       thoundBit.unshift(strNum.substring(index, lastIndex));
    }

    let str1 = startIndex == 1 ? String(num).charAt(0) : '',
        str2 = thoundBit.join(','),
        str3 = endIndex > -1 ? String(num).substring(endIndex) : '';
    return `${str1}${str2}${str3}`;
}
复制代码
thoundBitSeparator(-12514752232.21);   //-12,514,752,232.21
thoundBitSeparator(10);                //10

复制代码

今日头条急需前端开发。欢迎投递简历到 tcscyl@gmail.com / yanglei.yl@bytedance.com

分类:
前端
标签:
分类:
前端
标签:
收藏成功!
已添加到「」, 点击更改