js 修约

/**
 * 修约精度
 * @param num 数值
 * @param len 精度(1、0.5、10、0.1、0.01)等
 * @returns 四舍五入
 */
export function ROUND5(num, len) {
  return $ROUND(num, len, '5')
}

/**
 * 修约精度
 * @param num 数值
 * @param len 精度(1、0.5、10、0.1、0.01)等
 * @returns 四舍六入
 */
export function ROUND6(num, len) {
  return $ROUND(num, len, '6')
}

/**
 * 修约精度
 * @param num{数值}
 * @param len{精度(1、0.5、10、0.1、0.01)等}
 * @param even{修约(四舍五入、四舍六入)}
 * @returns {number}
 */
export function $ROUND(num: string | number, len: string | undefined, even: { toString: () => string | string[] } | undefined) {
  try {
    if (isNaN(Number(num))) {
      return num
    }
    num = numDecimal(num, 10)
    num = Number(num)
    var d = (len || 0).toString().trim() || 0
    var rule = even != undefined && (even.toString().indexOf('5') != -1 || even.toString().indexOf('五') != -1) ? 5 : 6
    if (len == '0.5') {
      //TODO 精度为 0.5 时
      //(0.75 * 2).修约 / 2
      if (rule == 5) num = fiveRound(num * 2, 0) / 2 //四舍五入
      else num = sixRound(num * 2, 0) / 2 //四舍六入
      //小数位自动补 "0"
      num = numDecimal(num, 1)
    } else if (len == '0.2') {
      //TODO 精度为 0.2 时
      //(0.75 * 5).修约 / 5
      if (rule == 5) num = fiveRound(num * 5, 0) / 5 //四舍五入
      else num = sixRound(num * 5, 0) / 5 //四舍六入
      //小数位自动补 "0"
      num = numDecimal(num, 1)
    } else if (len == '5') {
      //TODO 精度为 ”5“ 时 ,个位只会是(0 | 5)
      //(0.75 * 0.2).修约 / 0.2
      if (rule == 5) num = fiveRound(num * 0.2, 0) / 0.2 //四舍五入
      else num = sixRound(num * 0.2, 0) / 0.2 //四舍六入
      //小数位自动补 "0"
      num = numDecimal(num, 0)
    } else if (len == '10') {
      //TODO 精度为 ”10“ 时
      //(0.75 / 10).修约 * 10
      if (rule == 5) num = fiveRound(num / 10, 0) * 10 //四舍五入
      else num = sixRound(num / 10, 0) * 10 //四舍六入
    } else {
      //小数点后有几位,就保留几位小数,例:0.1、0.01
      if (len != undefined && len.toString().split('.').length > 1) d = len.toString().split('.')[1].length
      else d = 0
      if (rule == 5) num = fiveRound(num, d)
      else num = sixRound(num, d)
      //小数位自动补 "0"
      num = numDecimal(num, d)
    }
  } catch (e) {
    console.log('修约错误:' + e)
  }
  return num
}

/**
 * 四舍五入
 * @param num {数值}
 * @param decimalPlaces{小数位}
 * @returns {number}
 */
export function fiveRound(num: number, decimalPlaces: number) {
  var d = decimalPlaces || 0
  return Math.round(num * Math.pow(10, d)) / Math.pow(10, d)
}

/**
 * 四舍六入
 * @param num {数值}
 * @param decimalPlaces{小数位}
 * @returns {number}
 */
export function sixRound(num: number, decimalPlaces: number) {
  var d = decimalPlaces || 0
  var m = Math.pow(10, d)
  var n = +(d ? num * m : num).toFixed(8)
  var i = Math.floor(n),
    f = n - i
  var e = 1e-8
  var r = f > 0.5 - e && f < 0.5 + e ? (i % 2 == 0 ? i : i + 1) : Math.round(n)
  return d ? r / m : r
}

/**
 * 小数位不够自动补 “0”
 * @param num {数值}
 * @param len {保留几位小数}
 */
var numDecimal = function (num, len) {
  num = (num || 0).toString().trim() || 0
  len = (len || 0).toString().trim() || 0
  if (len == '0') return num.toString().split('.')[0]
  if (num.toString().indexOf('.') == -1 && len != 0) {
    num = num + '.0'
  }
  var split = num.toString().split('.')
  var decimal = split[1].toString()
  num = split[0].toString()
  if (decimal.length > len) {
    decimal = decimal.substring(0, len)
  } else {
    while (decimal.length != len) {
      decimal += '0'
    }
  }
  return num + '.' + decimal
}

 

posted @ 2023-08-09 10:17  岁月记忆  阅读(70)  评论(0编辑  收藏  举报