effect-io-ai

Package: effect
Module: BigInt

BigInt.toNumber

Converts a bigint to a number safely.

When to use

Use to convert a bigint to a JavaScript number only when it is a safe integer.

Details

If the bigint is outside the safe integer range for JavaScript (Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER), it returns Option.none().

Example (Converting bigints to numbers)

import { BigInt as BI } from "effect"

BI.toNumber(42n) // Option.some(42)
BI.toNumber(BigInt(Number.MAX_SAFE_INTEGER) + 1n) // Option.none()
BI.toNumber(BigInt(Number.MIN_SAFE_INTEGER) - 1n) // Option.none()

See

Signature

declare const toNumber: (b: bigint) => Option.Option<number>

Source

Since v2.0.0