vue实现将数字金额转换为大写人民币汉字

醉菜鸟 2022-06-24 08:00:11

/**
 * JS实现将数字金额转换为大写人民币汉字
 * @param $number
 * @returns {string|*}
 */
export function numRmb($number) {
  const $rmb_num = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
  const $big_unit = ['', '万', '亿', '兆']
  const $middle_unit = ['', '拾', '佰', '仟']
  const $small_unit = ['角', '分', '毫', '厘']
  let $int
  let $float
  let $nums
  let rmb = ''
  if ($number === '') return ''
  if (($number = parseFloat($number)) >= 1e15) return '超出最大处理数字'
  if ($number === 0) return $rmb_num[0] + '圆整'

  if (($number = $number.toString()).indexOf('.') === -1) {
    $int = $number
    $float = ''
  } else {
    $int = ($nums = $number.split('.'))[0]
    $float = $nums[1].substr(0, 4)
  }
  if (parseInt($int, 10) > 0) {
    let $count = 0
    const $int_len = $int.length
    let $i = 0
    for (; $i < $int_len; $i++) {
      const p = $int_len - $i - 1
      const h = p / 4
      const f = p % 4
      const $num = $int.substr($i, 1)
      if ($num === '0') {
        $count++
      } else {
        if ($count > 0) {
          rmb += $rmb_num[0]
        }
        $count = 0
        rmb += $rmb_num[parseInt($num)] + $middle_unit[f]
        if (f === 0 && $count < 4) {
          rmb += $big_unit[h]
        }
      }
    }
    rmb += '圆'
  }
  if ($float !== '') {
    const $float_let = $float.length
    let $i = 0
    for (; $i < $float_let; $i++) {
      const $num = $float.substr($i, 1)
      $num !== '0' && (rmb += $rmb_num[Number($num)] + $small_unit[$i])
    }
  }
  if (rmb === '') {
    rmb += $rmb_num[0] + '圆整'
  }
  if ($float === '') {
    rmb += '整'
  }
  return rmb
}
意见反馈