js小数运算,究极完善版!附数字转汉字

文章介绍了作者在处理小数运算时遇到精度问题,通过自定义函数decimalCalculation进行优化,同时提供了将阿拉伯数字转换为大写的ToLower函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

近期涉及到小数运算,在网上扒拉到类似很多这类的方法,刚开始使用没发现问题在这里插入图片描述
直到输入1111.11*100=111110.99999999999
在这里插入图片描述

好吧,只能自己改造一下了

/**
 * 小数计算
 * @param {*} num1 数字1
 * @param {*} num2 数字2
 * @param {*} symbol +-*\/符号
 * @returns 
 */
export function decimalCalculation(num1, num2, symbol) {
  const _number1 = _processingData(num1)
  const _number2 = _processingData(num2)
  const maxDigit = Math.max(_number1.digit, _number2.digit)
  const step = Math.pow(10, maxDigit)

  let result
  switch (symbol) {
    case '+':
      result = (_multiplication(_number1.split, maxDigit) + _multiplication(_number2.split, maxDigit)) / step
      break
    case '-':
      result = (_multiplication(_number1.split, maxDigit) - _multiplication(_number2.split, maxDigit)) / step
      break
    case '*':
      result = (_multiplication(_number1.split, maxDigit) * _multiplication(_number2.split, maxDigit)) / step / step
      break
    case '/':
      result = _multiplication(_number1.split, maxDigit) / _multiplication(_number2.split, maxDigit)
      break
    default:
      break
  }

  // 拆分小数,计算位数
  function _processingData(num) {
    const str = num.toString()
    const split = str.split('.')
    let digit = 0
    if (split[1]) {
      digit = split[1].length
    } else {
      split[1] = ''
    }
    return { str, split, digit }
  }

  // 字符串组装完成乘法运算,js* 1111.11*100会出现问题
  function _multiplication(num, digit) {
    return Number(num[0] + num[1].padEnd(digit, 0))
  }

  return result
}

可直接食用,下边附赠一份数字转汉字

// 阿拉伯数字转换大写
export function ToLower(str, capital = true) {
  const STR = String(str).split('.')

  let len = STR[0].length - 1
  if (STR[0] == 0) {
    return '零'
  }
  let idxs = ['', '拾', '百', '千', '万', '拾', '百', '千', '亿', '拾', '百', '千', '万', '拾', '百', '千', '亿']
  let num = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
  if (!capital) {
    idxs = ['', '十', '百', '千', '万', '十', '百', '千', '亿', '十', '百', '千', '万', '十', '百', '千', '亿']
    num = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
  }
  const integer = STR[0].replace(/([1-9]|0+)/g, function ($, $1, idx, full) {
    let pos = 0
    if ($1[0] != '0') {
      pos = len - idx
      if (idx == 0 && $1[0] == 1 && idxs[len - idx] == '十') {
        return idxs[len - idx]
      }
      return num[$1[0]] + idxs[len - idx]
    } else {
      let left = len - idx
      let right = len - idx + $1.length
      if (Math.floor(right / 4) - Math.floor(left / 4) > 0) {
        pos = left - (left % 4)
      }
      if (pos) {
        return idxs[pos] + num[$1[0]]
      } else if (idx + $1.length >= len) {
        return ''
      } else {
        return num[$1[0]]
      }
    }
  })
  if (STR.length > 1) {
    const decimal = STR[1].replace(/([1-9]|0+)/g, function ($, $1, idx, full) {
      return num[$1[0]]
    })
    return integer + '点' + decimal
  } else {
    return integer
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值