金额格式化,加千分位,保留几位小数

/* 金额格式化,加逗号 */
  function moneyFormat01(value, point) {
    point = point > 0 && point <= 20 ? point : 2;
    value =
      parseFloat((value + "").replace(/[^\d\.-]/g, "")).toFixed(point) + "";
    var valStr = value.split(".")[0].split("").reverse();
    var pointBehind = value.split(".")[1];
    var beforePoint = "";
    for (let i = 0; i < valStr.length; i++) {
      beforePoint +=
        valStr[i] + ((i + 1) % 3 == 0 && i + 1 != valStr.length ? "," : "");
    }
    return beforePoint.split("").reverse().join("") + "." + pointBehind;
  }
  console.log(moneyFormat01(-11123.909, 2), "数字金额化");

  /* 格式化金额  加千分位,保留相应小数 */
  function moneyFormat02(vlaue, point) {
    if (vlaue != ".") {
      let fff = Math.round(vlaue * 100) / 100;
      var s = fff.toString();
      let rs = s.indexOf(".");
      if (rs <= 0) {
        rs = s.length;
        s += ".";
      }
      while (s.length <= rs + point) {
        s += 0;
      }
    } else {
      let s = "0.00";
    }
    let pointBefore = "";
    if (s.substr(0, 1) == "-") {
      s = s.substr(1);
      pointBefore = "-";
    }
    let arr = s.split(".");
    let before01 = arr[0];
    let result = "",
      number = 0;
    before01 = (before01 || 0).toString();
    for (let i = before01.length - 1; i >= 0; i--) {
      number++;
      result = before01.charAt(i) + result;
      if (!(number % 3) && i != 0) {
        result = "," + result;
      }
    }
    if (arr.len == 1) {
      return pointBefore + result;
    } else {
      return pointBefore + result + "." + arr[1];
    }
  }
  console.log(moneyFormat02(12567345.9089, 3), "金额格式化");

 (2)删除金额得逗号和.00

// 删除金额逗号、“.00”的格式化方法
    cancelFormat (val) {
        return val.replace(/,|\s|-|(\.00)\b/g, '');
    },

 (3)第三种方式,但是不推荐,只能保留相应小数,但是保留两位小数时不能做到四舍五入

  /* 
   金钱格式化过滤器
   keepPoint==1的时候,不保留小数点  当keepPoint ===2 ,保留2位小数点,但是不四舍五入;其它的话,保留2位就是四舍五入。
   */

  function formatMoney(param, keepPoint) {
    let value;
    param = parseFloat(param);
    if (param === "" || isNaN(param)) {
      return 0;
    }
    // keepPoint==1的时候,不保留小数点  当keepPoint ===2 ,保留2位小数点,但是不四舍五入;其它的话,保留2位就是四舍五入。
    else if (keepPoint === 0) {
      value = parseFloat(param)
        .toFixed(0)
        .replace(/(\d)(?=(\d{3})+$)/g, "$1,");
    } else if (keepPoint === 1) {
      value = parseFloat(param)
        .toFixed(1)
        .replace(/(\d)(?=(\d{3})+$)/g, "$1,");
    } else if (keepPoint === 2) {
      value = param.toString();
      value =
        value.indexOf(".") > 0
          ? value.replace(/\.\d*/i, (xiaoshu) => {
              return xiaoshu.length === 2 ? xiaoshu + "0" : xiaoshu.slice(0, 3);
            })
          : param + ".00";
      value = value.replace(/(\d)(?=(\d{3})+\.)/g, "$1,");
    } else {
      value = parseFloat(param)
        .toFixed(keepPoint)
        .replace(/(\d)(?=(\d{3})+\.)/g, "$1,");
    }
    return value;
  }

  console.log(formatMoney("1234.45789", 2), "金钱格式化过滤?");

  

posted @ 2020-07-25 21:10  沁猿春  阅读(738)  评论(0编辑  收藏  举报