big.js库处理js精度丢失的问题,加法,乘法,除法,减法的使用

big.js库处理精度丢失的问题

# 使用 npm 安装
npm install big.js

# 如果是 TypeScript 项目,建议安装类型声明
npm install @types/big.js -D

使用 big.js 库,相乘

实现 0.00000007 和 10000 相乘,核心是调用实例的 .times() 方法。

<script lang="ts" setup>
import Big from 'big.js';

// 1. 使用字符串创建 Big 实例(推荐做法)
const num1 = new Big('0.0000000007');
const num2 = new Big('10000'); 
// 0.00000007 和 10000 相乘,调用.times()
const result = num1.times(num2);
console.log('1result==>', result);
// 需要使用toString进行转换一下哈
console.log('2result==>',result.toString());
</script>

image

乘法和链式调用与格式化

<script lang="ts" setup>
import Big from 'big.js';

// 直接链式调用,直接写成字符串
const result = Big('0.00000007').times('10000');
console.log('result==>', result.toString()); // "0.0007"
// 如果业务需要固定保留几位小数(例如保留 4 位小数),可以使用 toFixed
console.log('result保留4位小数==>', result.toFixed(4)); // "0.0007"
</script>

result> 0.0007
result保留4位小数
> 0.0007

big.js 的toFixed() 和Number.prototype.toFixed()行为不同

big.js 的toFixed() 完全不会舍入。不会出现精度丢失。只有你传入位数时才会舍入。
Number.prototype.toFixed()会4舍6入,5看情况。
通过代码来说明它们的不同

<script lang="ts" setup>
import Big from 'big.js';

let num3= 1.26;
//输出的是1.3, 原生的toFixed会进行四舍六入,五看情况
console.log('num3.toFixed(1):', num3.toFixed(1)); 

let num4= new Big('1.26');
// 用Big的toFixed完全不会舍入。不会出现精度丢失,只有你传入位数时才会舍入。
// 输出的是1.26
console.log('num4.toFixed():', num4.toFixed()); 
</script>
<style scoped >

image

加法 plus

<script lang="ts" setup>
import Big from 'big.js';

// 创建实例(推荐传入字符串以避免初始精度丢失)
const num1 = new Big('0.1');
const num2 = new Big('0.2');
// 2. 基础四则运算,使用plus进行加法
const sum = num1.plus(num2);       // 加法: 0.3
console.log('big.js加法结果:', sum.toString());
console.log('不使用big.js,加法:',0.1 + 0.2);
</script>

image

使用plus进行加法,toFixed解决出现科学计数法

<script lang="ts" setup>
import Big from 'big.js';
// 创建实例(推荐传入字符串以避免初始精度丢失)
const num1 = new Big('0.00000007');
const num2 =  Big('0.00000007');
const sum = num1.plus(num2);       
console.log('big.js加法结果,使用toFixed()方法:', sum.toFixed());
console.log('big.js加法结果,使用toString()方法:', sum.toString());
</script>

big.js加法结果,使用toFixed()方法: 0.00000014
big.js加法结果,使用toString()方法: 1.4e-7
image

除法

<script lang="ts" setup>
import Big from 'big.js';
// 创建实例(推荐传入字符串以避免初始精度丢失)
const num1 = new Big('1');
const num2 = new Big('10000000');
// 使用div进行除法
const quotient = num1.div(num2);       
console.log('big.js除法结果toFixed:', quotient.toFixed());
console.log('big.js除法结果toString:', quotient.toString());
console.log('不使用big.js,除法结果:',1/10000000);
</script>

big.js除法结果toFixed: 0.0000001
big.js除法结果toString: 1e-7
不使用big.js,除法结果: 1e-7

减法

<script lang="ts" setup>
import Big from 'big.js';
// 创建实例(推荐传入字符串以避免初始精度丢失)
const num1 = new Big('1.7777777777');
const num2 = new Big('1');
// 使用minus进行减法
const quotient = num1.minus(num2);       
console.log('big.js减法结果toFixed:', quotient.toFixed());
console.log('big.js减法结果toString:', quotient.toString());
console.log('不使用big.js,除法结果:',1/10000000); 
</script>

big.js减法结果toFixed: 0.7777777777
big.js减法结果toString: 0.7777777777
不使用big.js,除法结果: 1e-7

要不要使用new字符呢?

<script lang="ts" setup>
import Big from 'big.js';
// 创建实例(推荐传入字符串以避免初始精度丢失)
const num1 = new Big('1.7777777777');
// 'new' 关键字是可选的
const num2 = new  Big('1');
</script>

看了上面的代码我们会想,要不要使用new字符呢?
建议使用new字符。原因:
可读性 :new Big('1') 明确表达“创建一个实例”,
有个真实的坑 :Big() 不带参数调用(也没有new )返回的不是数字 0,
而是一个 全新的构造器函数 (用于创建独立配置的 Big)。
省略new 会放大这种“函数调用也能造实例”的歧义。

在实际开发中,什么情况下会使用new

在实际开发中,是否需要使用new字符的核心判断是:你需要一个各自独立,互不影响的实例。
1,自己写的class,必须new字符

class Cart { }
// class 不能当普通函数调用,会直接抛错
const cart = new Cart();  
  1. 需要多份互不干扰的实例,必须new字符
const timer1 = new Timer ( 1000 );
const timer2 = new Timer ( 3000 ); // 两个实例互不影响

big.js中toString会出现科学计数法,toFixed不会出现科学计数法

big.js中toString会出现科学计数法,toFixed不会出现科学计数法

big.js中toString会出现科学计数法,toFixed不会出现科学计数法

为什么推荐传字符串?

在处理极小或极大浮点数时,数字0.00000007
原生 JS 很容易出现类似 0.000000070000000001 的精度偏差。
使用字符串传入 big.js 可以从源头切断这种风险,确保计算绝对精准。

big.js库的兼容性怎么样

big.js 的兼容性非常优秀,放心大胆的使用。
浏览器兼容性:几乎无死角
底层标准:big.js 仅使用了 ECMAScript 3 标准编写。
实际表现:这意味着它能在所有浏览器中运行,包括非常古老的浏览器(如 IE6/7/8)。
现代环境:当然,在所有现代浏览器(Chrome、Firefox、Safari、Edge)中更是完美支持。

posted @ 2026-11-28 10:20  南风晚来晚相识  阅读(0)  评论(0)    收藏  举报