千分位,两位小数(是否保留)

/* fixed:是否保留两位小数(E5及以上) */
getThousandsNum = (value,fixed) => {
      var valueNum = Number(value);
      if(isNaN(valueNum)){
          return 0.00;
      }
      var reg = /\d{1,3}(?=(\d{3})+$)/g;
      var valueFloat = parseFloat(value);
      var valueFixed = valueFloat.toFixed(2);
      var valueArr = valueFixed.split('.');
      var valueIntThousands = valueArr[0].replace(reg,'$&,');
      if(fixed){
          return valueIntThousands+'.'+valueArr[1];
      }else{
          var arr = (valueFloat+'').split('.');
          return arr[0].replace(reg,'$&,')+(arr[1] ? '.'+arr[1] : '');
      }
      
  }
/* priceInt:小数点前几位
 * decimal:小数点后几位
 *小程序 (.wxs)
 */
transferThousandthsObj: function (num) {
    var numFloat = parseFloat((num || 0)).toFixed(2);
    var numInt = parseInt((numFloat || 0))+'';
    var footerNum = numFloat.substring(numFloat.length - 2, numFloat.length);
    var result = '';
    while (numInt.length > 3) {
        result = ',' + numInt.slice(-3) + result;
        numInt = numInt.slice(0, numInt.length - 3);
    }
    if (numInt) {
        result = numInt + result;
    }
    return {
      priceInt:result,
      decimal:footerNum,
    }
  }
posted @ 2020-08-31 23:48  胡姐姐  阅读(143)  评论(0编辑  收藏  举报