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

前端vue项目使用Decimal.js做加减乘除求余运算

本文介绍了如何在Vue项目中安装并使用Decimal.js库进行加、减、乘、除和求余等精确数值运算,以及注意事项:运算结果为Decimal对象需转换为数字。
安装Decimal
npm install decimal.js
引用
import Decimal  from 'decimal.js'
使用 

注意:运算结果是Decimal对象,需要使用.toNumber()转为数字

加 add 
import Decimal  from 'decimal.js'
const num1 = new Decimal(0.1);
const num2 = new Decimal(0.2);
const remainder = num1.add(num2).toNumber(); //0.3
console.log(remainder);
 减 sub
import Decimal  from 'decimal.js'
const num1 = new Decimal(5);
const num2 = new Decimal(3); 
const remainder = num1.sub(num2).toNumber(); //2
console.log(remainder); 
 乘 mul
import Decimal  from 'decimal.js'
const num1 = new Decimal(5);
const num2 = new Decimal(3);
const remainder = num1.mul(num2).toNumber(); //15
console.log(remainder);
除 div
import Decimal  from 'decimal.js'
const num1 = new Decimal(5);
const num2 = new Decimal(3);
const remainder = num1.div(num2).toNumber(); //1.6666666666666667
const remainder1 = num1.div(num2).toNumber().toFixed(2); //1.67 保留两位

console.log(remainder);
求余 modulo
import Decimal  from 'decimal.js'
const num1 = new Decimal(5);
const num2 = new Decimal(3);
const remainder = num1.modulo(num2).toNumber() //2
console.log(remainder);


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

相关文章:

  • C++20中头文件source_location的使用
  • 大数据学习-Clickhouse
  • 数据结构——链表,哈希表
  • makefile和make
  • JavaWeb学习(3)
  • [项目详解][boost搜索引擎#1] 概述 | 去标签 | 数据清洗 | scp
  • 024 elasticsearch集群
  • 生财合伙人推荐 - 鞠海深-群控
  • 霍夫圆型硬币检测Matlab程序
  • GitHub与GitCode
  • vuefor循环动态展示图片不显示
  • ARM指令集和汇编语言的关联学习
  • 设计模式——代理模式(6)
  • 408算法题leetcode--第33天
  • 【概率论】泊松分布
  • Gorm操作数据库,有和没有WithContext的区别
  • 【设计模式】深入理解 Python 单例模式:从原理到实现
  • 第8篇:网络安全基础
  • Docker 安装sql server 登陆失败
  • .NET Sqlite加密