index.wxs 2.29 KB
/**设置千分位 */
var setPrice = function (n = 0.0) {
  if ((typeof n == 'string' || typeof n == 'number') && !isNaN(n)) {
    n = parseFloat(n).toFixed(2)
    var r = getRegExp('(d{1,3})(?=(d{3})+(?:.))', 'g')
    var p = n.replace(r, ',')
    return p
    // var r = getRegExp('/./', 'g')
    // var p = n.replace(r, 'test')
    // return n
  } else {
    console.log(n, '不是一个数字', !isNaN(n))
  }
}

var threeNumForTwo = {
  money_three_for_two_thousands: function (num) {
    //num = parseInt(num);
    // return num
    var num = num + ''
    var d = ''
    if (num.slice(0, 1) == '-') {
      d = num.slice(0, 1)
      num = num.slice(1)
    }
    var len = num.length
    var index = num.indexOf('.')
    if (index == -1) {
      num = num + '.00'
    } else if (index + 2 == len) {
      num = num + '0'
    }
    var index = num.indexOf('.') // 字符出现的位置
    var num2 = num.slice(-3)
    num = num.slice(0, index)
    var result = ''
    while (num.length > 3) {
      result = ',' + num.slice(-3) + result
      num = num.slice(0, num.length - 3)
    }
    if (num) {
      result = num + result
    }
    // return result
    return d + (result + num2)
  }
}
var format = {
  yearFormat: function (date) {
    var _date = getDate(date)
    var year = _date.getFullYear()
    return year
  },
  dateFormat: function (date) {
    var _date = getDate(date)
    var year = _date.getFullYear()
    var month = _date.getMonth() + 1
    var day = _date.getDate()
    return year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day)
  },
  mileageFormat: function (mileage) {
    if (mileage > 10000) {
      return (mileage / 10000).toFixed(2) + '万公里'
    } else {
      return mileage + '公里'
    }
  },
  // 万单位,保留两位小数
  priceFormat: function(price) {
    return (price / 10000).toFixed(2)
  },
  // 万单位,加千位符
  threeNumForTwoPriceFormat: function(num) {
    return threeNumForTwo.money_three_for_two_thousands(format.priceFormat(num))
  }
}

/*
 * 导出
 */
module.exports = {
  setPrice: setPrice,
  money_three_for_two_thousands: threeNumForTwo.money_three_for_two_thousands, //暴露接口调用
  threeNumForTwoPriceFormat: format.threeNumForTwoPriceFormat,
  dateFormat: format.dateFormat,
  mileageFormat: format.mileageFormat,
}