快速入门
目标:10 分钟内跑通 decimal.js 的安装、导入与最常用的操作。
前置知识:JavaScript 基础。所有示例均基于 decimal.js 10.6.0 实测。
1. 安装
bash
npm install decimal.js浏览器可直接引入单个文件 decimal.js(全局变量 Decimal)或 ES 模块 decimal.mjs:
html
<script src='path/to/decimal.js'></script>
<script type="module">
import Decimal from './path/to/decimal.mjs';
</script>也可以从 CDN 引入(jsdelivr / cdnjs / unpkg)。
2. 导入
Node.js(CommonJS):
js
const Decimal = require('decimal.js');Node.js / 打包器(ES Module):
js
import Decimal from 'decimal.js';
// 或具名导入
import { Decimal } from 'decimal.js';TypeScript 直接可用,包内自带 decimal.d.ts。
3. 第一个程序
js
const Decimal = require('decimal.js');
// 构造:number / string / Decimal 实例都行
const x = new Decimal(123.4567);
const y = new Decimal('123456.7e-3'); // 字符串,科学计数法
const z = new Decimal(x); // 从 Decimal 构造
x.equals(y) && y.equals(z) && x.equals(z) // true,三者值相等4. 基本运算
js
new Decimal(0.3).minus(0.1) // '0.2' (原生 JS:0.3 - 0.1 === 0.19999999999999998)
new Decimal('0.1').plus('0.2') // '0.3' (原生 JS:0.1 + 0.2 === 0.30000000000000004)
new Decimal('1.5').times(2) // '3' 乘法
new Decimal(1).div(3) // '0.33333333333333333333' 除法,默认 20 位有效数字
new Decimal(10).mod(3) // '1' 取模
new Decimal(2).pow(10) // '1024' 幂
new Decimal(2).sqrt() // '1.4142135623730950488' 开平方- 方法不会修改原对象(不可变),返回新的
Decimal:
js
const x = new Decimal(0.3);
x.minus(0.1); // '0.2'
x; // '0.3',x 没有被改变- 返回
Decimal的方法可以链式调用:
js
new Decimal(2).div(3).plus(1).times(100).floor() // '166'5. 比较
js
new Decimal('1.0').eq(1) // true,'1.0' 与 1 相等
new Decimal(1).cmp(2) // -1 (小于)
new Decimal(2).cmp(1) // 1 (大于)
new Decimal(1).cmp(1) // 0 (相等)
new Decimal(1).lt(2) // true (<)
new Decimal(1).gt(2) // false(>)
new Decimal(2).gte(2) // true (>=)
new Decimal(2).lte(2) // true (<=)
// 判断类
new Decimal(0).isZero() // true
new Decimal('2.5').isInteger() // false
new Decimal('2.0').isInteger() // true
new Decimal(NaN).isNaN() // true
new Decimal(Infinity).isFinite() // false详见 比较与判断。
6. 格式化输出
js
new Decimal('255.5').toFixed(5) // '255.50000' 固定小数位(字符串)
new Decimal('255.5').toExponential(5) // '2.55500e+2' 科学计数法(字符串)
new Decimal('255.5').toPrecision(5) // '255.50' 指定位数(字符串)
new Decimal('1.235').toDP(2) // '1.24' 保留 2 位小数(Decimal)
new Decimal('1234.5').toSD(3) // '1230' 保留 3 位有效数字(Decimal)
new Decimal('1.5').toNumber() // 1.5 转原生 Number
new Decimal('1.5').toString() // '1.5' 转字符串WARNING
toString() 的指数阈值:绝对值 ≥ 10²¹ 或 ≤ 10⁻⁷ 时默认输出科学计数法。 new Decimal('0.0000001').toString() → '1e-7';想避免指数形式,用 toFixed()。 详见 格式化与输出。
7. 配置精度
默认 20 位有效数字、四舍五入(ROUND_HALF_UP)。全局配置:
js
Decimal.set({ precision: 5, rounding: 4 });
new Decimal(5).div(3) // '1.6667'(5 位有效数字)
// 独立配置:clone 出另一个构造器,互不影响
const Dec = Decimal.clone({ precision: 9, rounding: 1 });
new Dec(5).div(3) // '1.66666666'(9 位有效数字)
new Decimal(5).div(3) // '1.6667'(仍为全局 5 位)详见 配置与舍入模式。
8. 常用场景片段
金额计算(避免浮点误差):
js
// 商品总价:0.1 元 × 3 + 0.2 元
const total = new Decimal('0.1').times(3).plus('0.2');
total.toFixed(2) // '0.50'
total.toNumber() // 0.5百分比:
js
new Decimal('50').div(200).times(100) // '25'(25%)大整数运算(超过 2⁵³ = 9007199254740992):
js
new Decimal('9007199254740993').plus(1) // '9007199254740994'
9007199254740993 + 1 // 9007199254740992(原生 Number 已出错)比较两个浮点运算结果:
js
new Decimal('0.1').plus('0.2').eq('0.3') // trueJSON 序列化(toJSON 返回字符串,不会丢精度):
js
JSON.stringify({ price: new Decimal('19.99') })
// '{"price":"19.99"}'