Hyperlinkv0.8.0-beta.28

BigDecimal

BigDecimal.fromNumberUnsafeconsteffect/BigDecimal.ts:1290
(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))
constructorsfromNumber
export const fromNumberUnsafe = (n: number): BigDecimal => {
  return Option.getOrThrowWith(fromNumber(n), () => new RangeError(`Number must be finite, got ${n}`))
}