千位分隔符實現

千位分隔符實現

 千位分隔符實現很常見,例如-1,233,552,214,233.214;javascript

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前端

相關文章
相關標籤/搜索