(n: number): BigDecimalCreates a BigDecimal from a finite number.
When to use
Use when you need to convert a trusted finite JavaScript number to a
BigDecimal and want a plain result instead of an Option.
Gotchas
It is not recommended to convert a floating point number to a decimal
directly, as the floating point representation may be unexpected. Throws a
RangeError if the number is not finite (NaN, +Infinity or -Infinity).
Example (Creating decimals from finite numbers)
import { BigDecimal } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(BigDecimal.fromNumberUnsafe(123), BigDecimal.make(123n, 0))
assert.deepStrictEqual(BigDecimal.fromNumberUnsafe(123.456), BigDecimal.make(123456n, 3))export const const fromNumberUnsafe: (
n: number
) => BigDecimal
Creates a BigDecimal from a finite number.
When to use
Use when you need to convert a trusted finite JavaScript number to a
BigDecimal and want a plain result instead of an Option.
Gotchas
It is not recommended to convert a floating point number to a decimal
directly, as the floating point representation may be unexpected. Throws a
RangeError if the number is not finite (NaN, +Infinity or -Infinity).
Example (Creating decimals from finite numbers)
import { BigDecimal } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(BigDecimal.fromNumberUnsafe(123), BigDecimal.make(123n, 0))
assert.deepStrictEqual(BigDecimal.fromNumberUnsafe(123.456), BigDecimal.make(123456n, 3))
fromNumberUnsafe = (n: numbern: number): BigDecimal => {
return import OptionOption.const getOrThrowWith: {
(onNone: () => unknown): <A>(
self: Option<A>
) => A
<A>(self: Option<A>, onNone: () => unknown): A
}
getOrThrowWith(const fromNumber: (
n: number
) => Option.Option<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())
fromNumber(n: numbern), () => new var RangeError: RangeErrorConstructor
new (message?: string, options?: ErrorOptions) => RangeError (+3 overloads)
RangeError(`Number must be finite, got ${n: numbern}`))
}