Skip to content

加法 add


Show Source Code
ts
/**
 * 消除 js 加减精度问题的加法函数
 *
 * 该函数旨在添加两个数字,考虑到它们可能是整数或小数;对于整数,直接返回它们的和
 * 对于小数,为了确保精确计算,将小数转换为相同长度的字符串进行处理,然后将结果转换回小数
 *
 * @param num1 第一个数字
 * @param num2 第二个数字
 * @returns 返回两个数字的和
 */
export function add(num1: number, num2: number): number {
  // 验证输入是否为有效的数字
  // Number.isNaN() 不会尝试将参数转换为数字;全局 isNaN() 函数会将参数强制转换为数字
  if (Number.isNaN(num1) || Number.isNaN(num2)) {
    throw new Error('Both num1 and num2 must be valid numbers.')
  }
  // 检查输入是否为小数
  const isDecimalNum1 = num1 % 1 !== 0
  const isDecimalNum2 = num2 % 1 !== 0
  if (!isDecimalNum1 && !isDecimalNum2) {
    return num1 + num2 // 如果两个数字都是整数,则直接返回它们的和
  }
  const num1DeciStr = String(num1).split('.')[1] ?? ''
  const num2DeciStr = String(num2).split('.')[1] ?? ''
  const maxLen = Math.max(num1DeciStr.length, num2DeciStr.length)
  const factor = Math.pow(10, maxLen)
  const num1Str = num1.toFixed(maxLen)
  const num2Str = num2.toFixed(maxLen)
  // 将小数点移除并转换为整数相加
  const result = (+num1Str.replace('.', '') + +num2Str.replace('.', '')) / factor
  return result
}

何时使用

  • 消除 js 加减精度问题时

基本使用

vue
<script setup lang="ts">
import { add } from 'vue-amazing-ui'

console.log(0.1 + 0.2) // js直接计算结果: 0.30000000000000004
add(0.1, 0.2) // 0.3
</script>

Params

参数说明类型默认值
num1加数1numberundefined
num2加数2numberundefined

Released under the MIT License.