(n: BigDecimal): numberConverts a BigDecimal to a JavaScript number.
When to use
Use when you need a JavaScript number at an interop boundary where precision loss is acceptable.
Gotchas
This conversion is unsafe because the result can lose integer or fractional
precision, round to a nearby representable value, or become Infinity when
the decimal cannot be represented as a finite JavaScript number.
Example (Converting decimals to numbers)
import { BigDecimal } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(BigDecimal.toNumberUnsafe(BigDecimal.fromStringUnsafe("123.456")), 123.456)export const const toNumberUnsafe: (
n: BigDecimal
) => number
Converts a BigDecimal to a JavaScript number.
When to use
Use when you need a JavaScript number at an interop boundary where precision
loss is acceptable.
Gotchas
This conversion is unsafe because the result can lose integer or fractional
precision, round to a nearby representable value, or become Infinity when
the decimal cannot be represented as a finite JavaScript number.
Example (Converting decimals to numbers)
import { BigDecimal } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(BigDecimal.toNumberUnsafe(BigDecimal.fromStringUnsafe("123.456")), 123.456)
toNumberUnsafe = (n: BigDecimal(parameter) n: {
value: bigint;
scale: number;
normalized: BigDecimal;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
n: BigDecimal): number => var Number: NumberConstructor
;(value?: any) => number
An object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.
Number(const format: (n: BigDecimal) => stringFormats a BigDecimal as a string.
When to use
Use to render a BigDecimal as plain decimal text when possible.
Details
The value is normalized before formatting. Scientific notation is used when
the absolute value of the normalized scale is at least 16; otherwise plain
decimal notation is used.
Example (Formatting decimals)
import { BigDecimal } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(BigDecimal.format(BigDecimal.fromStringUnsafe("-5")), "-5")
assert.deepStrictEqual(BigDecimal.format(BigDecimal.fromStringUnsafe("123.456")), "123.456")
assert.deepStrictEqual(BigDecimal.format(BigDecimal.fromStringUnsafe("-0.00000123")), "-0.00000123")
format(n: BigDecimal(parameter) n: {
value: bigint;
scale: number;
normalized: BigDecimal;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
n))