js 带两位小数的数字转中文数字大写

写得比较累赘,见谅

//解析开票金额

function encodeAmount(data) {
  let uppercase = '' //大写
  let init = String(data).split('.')[0] //整数长度
  let double = String(data).split('.')[1] //小数部分
  let str = String(init)
  let strSecation = [] //数字段
  let n = 4
  for (let i = 0, l = str.length; i < l / n; i++) {
    strSecation.push(
      str
        .split('')
        .reverse()
        .join('')
        .slice(n * i, n * (i + 1))
        .split('')
        .reverse()
        .join('')
    )
  }
let resArr = zhuanhuan(strSecation)
uppercase = deRepeat(resArr)
uppercase = double * 100 > 0 ? uppercase + doubleEncode(double) : uppercase + '整'
return uppercase;
}

//小数转译
function doubleEncode(doubleNumber) {
  let encodeMac = [
    { type: '0', value: '零' },
    { type: '1', value: '壹' },
    { type: '2', value: '贰' },
    { type: '3', value: '叁' },
    { type: '4', value: '肆' },
    { type: '5', value: '伍' },
    { type: '6', value: '陆' },
    { type: '7', value: '柒' },
    { type: '8', value: '捌' },
    { type: '9', value: '玖' },
  ]
  let res = ''
  let doubleStirng = String(doubleNumber).length == 2 ? String(doubleNumber) : '0' + String(doubleNumber)
  doubleStirng.split('').map((dItem, index) => {
    encodeMac.map((item) => {
      dItem == item.type && index == 0 ? (res += item.value + '角') : ''
      dItem == item.type && index == 1 && dItem !== '0' ? (res += item.value + '分') : ''
    })
  })
  return res
}

//整数调整
function deRepeat(Arr) {
  let resEnd = ''
  for (let k of Arr) {
    if (k.indexOf('零零零亿') > -1) {
      resEnd += k.replace(/零零零亿/g, '亿')
    } else if (k.indexOf('零零零万') > -1) {
      resEnd += k.replace(/零零零万/g, '万')
    } else if (k.indexOf('零零零圆') > -1) {
      resEnd += k.replace(/零零零圆/g, '圆')
    } else if (k.indexOf('零零亿') > -1) {
      resEnd += k.replace(/零零亿/g, '亿')
    } else if (k.indexOf('零零万') > -1) {
      resEnd += k.replace(/零零万/g, '万')
    } else if (k.indexOf('零零圆') > -1) {
      resEnd += k.replace(/零零圆/g, '圆')
    } else if (k.indexOf('零亿') > -1) {
      resEnd += k.replace(/零亿/g, '亿')
    } else if (k.indexOf('零万') > -1) {
      resEnd += k.replace(/零万/g, '万')
    } else if (k.indexOf('零圆') > -1) {
      resEnd += k.replace(/零圆/g, '圆')
    } else if (k.indexOf('零零零') > -1) {
      resEnd += k.replace(/零零零/g, '零')
    } else if (k.indexOf('零零') > -1) {
      resEnd += k.replace(/零零/g, '零')
    } else {
      resEnd += k
    }
  }
  if (resEnd.indexOf('亿万圆') > -1) {
    resEnd = resEnd.replace(/亿万圆/g, '亿圆')
  }
  return resEnd
}

//整数转译
function zhuanhuan(arr) {
  let res = []
  let encodeMac = [
    { type: '0', value: '零' },
    { type: '1', value: '壹' },
    { type: '2', value: '贰' },
    { type: '3', value: '叁' },
    { type: '4', value: '肆' },
    { type: '5', value: '伍' },
    { type: '6', value: '陆' },
    { type: '7', value: '柒' },
    { type: '8', value: '捌' },
    { type: '9', value: '玖' },
  ]
  let unit = [
    { type: 1, value: '' },
    { type: 2, value: '拾' },
    { type: 3, value: '佰' },
    { type: 4, value: '仟' },
    { type: 5, value: '万' },
    { type: 6, value: '拾万' },
    { type: 7, value: '佰万' },
    { type: 8, value: '仟万' },
    { type: 9, value: '亿' },
  ]
  for (let arrK in arr) {
    let initStrArr = arr[arrK].split('').reverse()
    let initStr = []
    initStrArr.map((item, index) => {
      if (index === 0) {
        initStr.push(String(item))
      } else {
        initStr.push(String(item * 10 ** index))
      }
    })
    initStr = initStr.reverse()

    let paramsArr = []
    for (let v of initStr) {
      let fir = v.split('')[0]
      let len = v.split('').filter((item) => item === '0').length + 1
      paramsArr.push({ fir: fir, len: len })
    }

    let resOver = []

    paramsArr.map((itemP, Pindex) => {
      encodeMac.map((item) => {
        if (itemP.fir === item.type) {
          resOver.push(Pindex == 3 && item.value == '零' ? '' : item.value)
          resOver.push(item.value == '零' ? '' : unit[itemP.len - 1].value)
        }
      })
    })

    let endUnit = ''
    switch (arrK) {
      case '0':
        endUnit = '圆'
        break
      case '1':
        endUnit = '万'
        break
      case '2':
        endUnit = '亿'
        break
      case '3':
        endUnit = '兆'
        break

      default:
        break
    }
    resOver = resOver.join('') + endUnit
    res.unshift(resOver)
  }
  return res
}

//只兼容了角和分
encodeAmount(127.56)
encodeAmount('15717.87')

你可能感兴趣的:(javascript前端转码)