Hyperlinkv0.8.0-beta.28

BigInt

BigInt.fromNumberfunctioneffect/BigInt.ts:912
(n: number): Option.Option<bigint>

Converts a number to a bigint.

When to use

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

Details

If the number is outside the safe integer range for JavaScript (Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER) or if the number is not a valid bigint, it returns Option.none().

Example (Converting numbers to bigints)

import { BigInt } from "effect"

BigInt.fromNumber(42) // Option.some(42n)

BigInt.fromNumber(Number.MAX_SAFE_INTEGER + 1) // Option.none()
BigInt.fromNumber(Number.MIN_SAFE_INTEGER - 1) // Option.none()
convertingtoNumberBigInt
Source effect/BigInt.ts:91211 lines
export function fromNumber(n: number): Option.Option<bigint> {
  if (n > Number.MAX_SAFE_INTEGER || n < Number.MIN_SAFE_INTEGER) {
    return Option.none()
  }

  try {
    return Option.some(BigInt(n))
  } catch {
    return Option.none()
  }
}