速查表(Cheat Sheet)
decimal.js 10.6.0 全 API 一页速查。所有输出为实测结果。
约定:🟢 返回Decimal(可链式)|🔵 返回字符串 |⚪ 返回 number/boolean
安装与导入
bash
npm install decimal.jsjs
const Decimal = require('decimal.js'); // CommonJS
import Decimal from 'decimal.js'; // ESM
import { Decimal } from 'decimal.js'; // ESM 具名默认配置
| 项 | 默认值 | 说明 |
|---|---|---|
precision | 20 | 有效数字位数(1~1e9) |
rounding | 4(ROUND_HALF_UP) | 舍入模式 |
modulo | 1(ROUND_DOWN) | 取模模式 |
toExpNeg / toExpPos | -7 / 21 | toString 指数阈值 |
minE / maxE | -9e15 / 9e15 | 下溢/溢出阈值 |
crypto | false | random 是否加密 |
舍入模式
| 常量 | 值 | 含义 |
|---|---|---|
ROUND_UP | 0 | 远离零 |
ROUND_DOWN | 1 | 趋向零(截断) |
ROUND_CEIL | 2 | 向 +∞ |
ROUND_FLOOR | 3 | 向 -∞ |
ROUND_HALF_UP | 4 | 四舍五入(默认) |
ROUND_HALF_DOWN | 5 | 五舍六入 |
ROUND_HALF_EVEN | 6 | .5 取偶数(银行家) |
ROUND_HALF_CEIL | 7 | .5 向 +∞ |
ROUND_HALF_FLOOR | 8 | .5 向 -∞ |
EUCLID | 9 | 取模专用:结果恒非负 |
实例方法
| 方法 | 别名 | 返回 | 说明 |
|---|---|---|---|
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() | — | ⚪bool | NaN |
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.precision、Decimal.rounding、Decimal.toExpNeg、Decimal.toExpPos、Decimal.minE、Decimal.maxE、Decimal.crypto、Decimal.modulo、Decimal.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 < y | x.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 * 2 | x.plus(1) / x.times(2) |
new Decimal(1e30) | new Decimal('1e30') |
官方资源
- GitHub:MikeMcl/decimal.js
- 完整 API 文档:mikemcl.github.io/decimal.js
- npm:decimal.js
- 轻量版(无三角函数):decimal.js-light
