Package: effect
Module: BigDecimal
Creates a BigDecimal safely from a finite number.
When to use
Use to convert a finite JavaScript number to a BigDecimal without throwing
on invalid input.
Details
Returns Option.none() for NaN, +Infinity or -Infinity.
Gotchas
It is not recommended to convert a floating point number to a decimal directly, as the floating point representation may be unexpected.
Example (Creating decimals from numbers safely)
import { BigDecimal, Option } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(BigDecimal.fromNumber(123), Option.some(BigDecimal.make(123n, 0)))
assert.deepStrictEqual(
BigDecimal.fromNumber(123.456),
Option.some(BigDecimal.make(123456n, 3))
)
assert.deepStrictEqual(BigDecimal.fromNumber(Infinity), Option.none())
See
fromNumberUnsafe for throwing when the number is not finitefromString for parsing decimal strings directlySignature
declare const fromNumber: (n: number) => Option.Option<BigDecimal>
Since v2.0.0