Skip to content

速查表(Cheat Sheet)

decimal.js 10.6.0 全 API 一页速查。所有输出为实测结果。
约定:🟢 返回 Decimal(可链式)|🔵 返回字符串 |⚪ 返回 number/boolean

安装与导入

bash
npm install decimal.js
js
const Decimal = require('decimal.js');          // CommonJS
import Decimal from 'decimal.js';               // ESM
import { Decimal } from 'decimal.js';           // ESM 具名

默认配置

默认值说明
precision20有效数字位数(1~1e9)
rounding4(ROUND_HALF_UP)舍入模式
modulo1(ROUND_DOWN)取模模式
toExpNeg / toExpPos-7 / 21toString 指数阈值
minE / maxE-9e15 / 9e15下溢/溢出阈值
cryptofalserandom 是否加密

舍入模式

常量含义
ROUND_UP0远离零
ROUND_DOWN1趋向零(截断)
ROUND_CEIL2向 +∞
ROUND_FLOOR3向 -∞
ROUND_HALF_UP4四舍五入(默认)
ROUND_HALF_DOWN5五舍六入
ROUND_HALF_EVEN6.5 取偶数(银行家)
ROUND_HALF_CEIL7.5 向 +∞
ROUND_HALF_FLOOR8.5 向 -∞
EUCLID9取模专用:结果恒非负

实例方法

方法别名返回说明
plus(n)add🟢
minus(n)sub🟢
times(n)mul🟢
dividedBy(n)div🟢除(1/0 → Infinity)
dividedToIntegerBy(n)divToInt🟢整除(截断)
modulo(n)mod🟢取模(符号规则看配置)
toPower(n)pow🟢幂(支持非整数)
squareRoot()sqrt🟢平方根
cubeRoot()cbrt🟢立方根(10.6)
absoluteValue()abs🟢绝对值
negated()neg🟢取负
round()🟢按全局 rounding 取整(无参数
floor()🟢向下取整
ceil()🟢向上取整
trunc()truncated🟢截断
clamp(min, max)clampedTo🟢范围钳制(10.6)
toDP(n, mode?)toDecimalPlaces🟢按小数位舍入(n ≥ 0)
toSD(n, mode?)toSignificantDigits🟢按有效数字舍入
toNearest(n?, mode?)🟢舍入到 n 的倍数
toFraction(maxDen?)🟢[]转分数 [分子, 分母]
comparedTo(n)cmp⚪-1/0/1比较(NaN → NaN)
equals(n)eq⚪bool相等
greaterThan(n)gt⚪bool>
greaterThanOrEqualTo(n)gte⚪bool
lessThan(n)lt⚪bool<
lessThanOrEqualTo(n)lte⚪bool
isFinite()⚪bool有限
isNaN()⚪boolNaN
isInteger()isInt⚪bool整数
isZero()⚪bool零(含 -0)
isNegative()isNeg⚪bool负(含 -0;0 不是负)
isPositive()isPos⚪bool正(0 视为正)
decimalPlaces()dp⚪number小数位(不计尾零)
precision()sd⚪number有效数字(sd(true) 计尾零)
e⚪number指数(只读属性)
toString()🔵十进制字符串
valueOf()🔵同 toString,但 -0 → '-0'
toJSON()🔵同 toString(JSON.stringify 用)
toNumber()⚪number转 Number(可能丢精度)
toFixed(n?, mode?)🔵固定小数位字符串(补零)
toExponential(n?, mode?)🔵科学计数法字符串
toPrecision(n?, mode?)🔵有效数字字符串
toBinary(n?, mode?)🔵二进制(0b 前缀)
toOctal(n?, mode?)🔵八进制(0o 前缀)
toHex(n?, mode?)toHexadecimal🔵十六进制(0x 前缀)
sin() / cos() / tan()sine/cosine/tangent🟢三角函数(弧度)
asin() / acos() / atan()inverse*🟢反三角
sinh() / cosh() / tanh()hyperbolic*🟢双曲函数
asinh() / acosh() / atanh()inverseHyperbolic*🟢反双曲
ln()naturalLogarithm🟢自然对数
log(base?)logarithm🟢任意底对数
exp()naturalExponential🟢e^x

静态方法

方法返回说明
Decimal(value)🟢构造(可不带 new)
Decimal.isDecimal(v)⚪bool是否为 Decimal
Decimal.add/sub/mul/div/mod(a, b)🟢四则/取模
Decimal.pow(b, e)🟢
Decimal.sqrt(x) / Decimal.cbrt(x)🟢开方/开立方
Decimal.abs/ceil/floor/round/trunc(x)🟢取整族
Decimal.clamp(x, min, max)🟢钳制(10.6)
Decimal.max/min(...n)🟢最大/最小(可变参数
Decimal.sum(...n)🟢求和(可变参数
Decimal.hypot(...n)🟢平方和开方(10.5)
Decimal.atan2(y, x)🟢四象限反正切(10.5)
Decimal.sin/cos/tan/...🟢全部三角/双曲函数
Decimal.ln(x) / Decimal.log(x, base?)🟢对数
Decimal.log10(x) / Decimal.log2(x)🟢常用/二进制对数(10.5)
Decimal.exp(x)🟢指数
Decimal.random(n?)🟢[0,1) 随机数
Decimal.sign(x)⚪-1/0/1符号
Decimal.set(config)构造器配置(别名 config
Decimal.clone(config?)构造器独立配置构造器

静态只读属性

Decimal.precisionDecimal.roundingDecimal.toExpNegDecimal.toExpPosDecimal.minEDecimal.maxEDecimal.cryptoDecimal.moduloDecimal.ROUND_*Decimal.EUCLID

最常用的 20 个示例

js
new Decimal('0.1').plus('0.2')                 // '0.3'
new Decimal(1).div(3)                          // '0.33333333333333333333'
new Decimal('1.5').times(2)                    // '3'
new Decimal(10).mod(3)                         // '1'
new Decimal(2).pow(0.5)                        // '1.4142135623730950488'
new Decimal(2).sqrt()                          // '1.4142135623730950488'
new Decimal(27).cbrt()                         // '3'
new Decimal('2.5').toDP(2)                     // '2.5'
new Decimal('1.235').toSD(3)                   // '1.24'
new Decimal('1.005').toFixed(2)                // '1.01'
new Decimal('255.5').toExponential(3)          // '2.555e+2'
new Decimal('255.5').toPrecision(4)            // '255.5'
new Decimal('1.2').toNearest(0.5)              // '1'
new Decimal('1.5').toFraction()                // [ '3', '2' ]
new Decimal(1).cmp(2)                          // -1
new Decimal('1.0').eq(1)                       // true
Decimal.max(1, 3, 2)                           // '3'
Decimal.sum(1, 2, 3)                           // '6'
Decimal.hypot(3, 4)                            // '5'
Decimal.sqrt('6.98372465832e+9823')            // '8.3568682281821340204e+4911'

反模式速查(不要这样写)

❌ 错误写法✅ 正确写法
new Decimal(0.1).plus(0.2)new Decimal('0.1').plus('0.2')
x == y / x === y / x < yx.eq(y) / x.cmp(y) / x.lt(y)
Decimal.max(arr)Decimal.max(...arr)
x.round(0, ROUND_DOWN)x.toDP(0, Decimal.ROUND_DOWN)
x + 1 / x * 2x.plus(1) / x.times(2)
new Decimal(1e30)new Decimal('1e30')

官方资源