当前位置: 首页 > article >正文

vue 使用decimal.js 解决小数相加合计精确度丢失问题

  • 安装依赖 decimal.js
    • npm install --save decimal.js
  • 封装
    • 在utils文件夹下创建decimal.js文件
import { Decimal } from 'decimal.js'
export function add (x, y) {
    if (!x) {
        x = 0
    }
    if (!y) {
        y = 0
    }
    const xx = new Decimal(x)
    const yy = new Decimal(y)
    return xx.plus(yy).toNumber()
}
// 减
export function sub (x, y) {
    if (!x) {
        x = 0
    }
    if (!y) {
        y = 0
    }
    const xx = new Decimal(x)
    const yy = new Decimal(y)
    return xx.sub(yy).toNumber()
}
// 除
export function div (x, y) {
    if (!x) {
        x = 0
    }
    if (!y) {
        return 0
    }
    const xx = new Decimal(x)
    const yy = new Decimal(y)
    return xx.div(yy).toNumber()
}
//乘
export function mul (x, y) {
    if (!x) {
        x = 0
    }
    if (!y) {
        y = 0
    }
    const xx = new Decimal(x)
    const yy = new Decimal(y)
    return xx.mul(yy).toNumber()
}
  • 页面使用

<script>
  import {add} from "@/utils/decimal"
  export default {
    methods:{
      handlePlus() {
        add(10.5,4.877)
      }
    }
  }
</script>

http://www.kler.cn/news/155823.html

相关文章:

  • 强化学习------时序差分(Temporal-Difference Learning)
  • 【开源】基于Vue.js的超市账单管理系统的设计和实现
  • Mybatis使用注解实现复杂动态SQL
  • 【CVE-2023-49103】ownCloud graphapi信息泄露漏洞(2023年11月发布)
  • 栈和队列的OJ题--13.用队列实现栈
  • java_基础——ArrayList
  • Spring一些基础问题整理
  • 谱方法学习笔记-下(超详细)
  • 基于Java SSM框架+Vue实现旅游资源网站项目【项目源码+论文说明】计算机毕业设计
  • 【云原生Prometheus篇】Prometheus PromQL语句详解 1.0
  • 使用idea中的Live Templates自定义自动生成Spring所需的XML配置文件格式
  • Redis部署脚本(完成-第一版)
  • shell命令编写
  • 正则表达式从放弃到入门(2):grep命令详解
  • 机器学习---pySpark代码开发
  • 实体类转SQL工具类
  • 在南昌找工作应该上什么网
  • MySQL更改用户权限
  • GitHub Actions 之自动化发布 Maven 项目
  • 也可Adobe Animate
  • C++代码规范(JSF-AV版本)未完待续
  • JavaWeb 添加页面和用户图像展示
  • NAND Flash和NOR Flash的异同
  • 芋道源码ruoyi-vue-pro项目前端yarn下载报错
  • LeedCode刷题---子数组问题
  • 数据库设计之三范式
  • 【计算机视觉】基于OpenCV计算机视觉的摄像头测距技术设计与实现
  • vue中keep-alive的使用
  • 【华为OD题库-049】评论转换输出-java
  • Android 13.0 Camera2 静音时拍照去掉快门声音