Hyperlinkv0.8.0-beta.28

BigInt

BigInt.toNumberconsteffect/BigInt.ts:838
(b: bigint): Option.Option<number>

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()
convertingfromNumber
Source effect/BigInt.ts:8386 lines
export const toNumber = (b: bigint): Option.Option<number> => {
  if (b > BigInt(Number.MAX_SAFE_INTEGER) || b < BigInt(Number.MIN_SAFE_INTEGER)) {
    return Option.none()
  }
  return Option.some(Number(b))
}