方式一

function formatMoney (num) {
    if (num) {
      num = num.toString().replace(/\$|,/g, '');
      if ('' == num || isNaN(num)) { return 'Not a Number ! '; }
      var sign = num.indexOf("-") >= 0 ? '-' : '';
      var cents = num.indexOf(".") > 0 ? num.substr(num.indexOf(".")) : '';
      cents = cents.length > 1 ? cents : '';
      if (sign == '') {
          num = num.indexOf(".") > 0 ? num.substring(0, (num.indexOf("."))) : num;
      }
      else {
          num = num.indexOf(".") > 0 ? num.substring(1, (num.indexOf("."))) : num.substring(1);
      }
      if ('' == cents) { if (num.length > 1 && '0' == num.substr(0, 1)) { return 'Not a Number ! '; } }
      else { if (num.length > 1 && '0' == num.substr(0, 1)) { return 'Not a Number ! '; } }
      for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3) ; i++) {
          num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
      }
      return (sign + num + cents);
    }
    else { return '0'; }
},

方式二

function formatMoney(value) {
    let newValue;
    if (!value && value !== 0) {
        return '';
    }
    value = value + '';
    if (value.includes('.')) {
        let newMoneyValue = value.split('.');
        newValue = newMoneyValue[0].replace(/\d{1,3}(?=(\d{3})+$)/g, '$&,') + '.' + newMoneyValue[1];
    } else {
        newValue = value.replace(/\d{1,3}(?=(\d{3})+$)/g, '$&,');
    }
    return newValue;
}

方式三

function formatMoney(num){
  num=num.toString().split(".");  // 分隔小数点
  var arr=num[0].split("").reverse();  // 转换成字符数组并且倒序排列
  var res=[];
  for(var i=0,len=arr.length;i<len;i++){
    if(i%3===0&&i!==0){
    res.push(",");   // 添加分隔符
  }
  	res.push(arr[i]);
  }
  res.reverse(); // 再次倒序成为正确的顺序
  if(num[1]){  // 如果有小数的话添加小数部分
  	res=res.join("").concat("."+num[1]);
  }else{
  	res=res.join("");
  }
  return res;
}

方式四

function formatMoney(item){
  if(typeof item === 'number' || item>1000 || item<-1000){
  	item = item.toString().replace(/\B(?=(\d{3})+\b)/g,',')
  }
  return item
}

方式五

function formatMoney(number, places,thousand, decimal) {
  number = number || 0;
  places = !isNaN(places = Math.abs(places)) ? places : 2;
  thousand = thousand || ",";
  decimal = decimal || ".";
  var negative = number < 0 ? "-" : "",
  i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + "",
  j = (j = i.length) > 3 ? j % 3 : 0;
  return   negative + (j ? i.substr(0, j) + thousand : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousand) + (places ? decimal + Math.abs(number - i).toFixed(places).slice(2) : "");
}

方式六

function formatMoney(num) {
  const str = num.toString()
  const reg = str.indexOf('.') > -1 ? /(\d{1,3})(?=(\d{3})+\.)/g : /(\d{1,3})(?=(\d{3})+$)/g;
  return str.replace(reg, '$1,')
}

方式七:根据 MDN 解释, number.toLocaleString() 方法返回这个数字number在特定语言环境下的表示字符串。

let num = 123456789097.7675;
num.toLocaleString() // "123,456,789,097.768"

方式八

function numberFormat(num) {
    let result = '';
    let count = 1;
    let nums = num.toString();
    for (let i = nums.length - 1; i >= 0; i--) {
        result = nums.charAt(i) + result;
        if (!(count % 3) && i != 0) { 
            result = ',' + result; 
        }
        count++;
    }
    return result;
}

方式九:此正则不含小数,因此不支持含有小数的数值

String(123456789097).replace(/(\d)(?=(\d{3})+$)/g, "$1,");

记录相关文档

https://www.cnblogs.com/linjunfu/p/10761504.html

https://www.cnblogs.com/caiyingyong/p/7395621.html

posted on 2022-08-20 16:48  羽丫头不乖  阅读(21)  评论(0编辑  收藏  举报