Try it Live
Run Uint examples in the interactive playground
Safe Integer Range
Usage Patterns
Array Indices
Safe Conversion
See Also
- fromNumber - Create from number
- toBigInt - Convert to BigInt (no limits)
Convert Uint256 to JavaScript number
// Safe values
Uint.toNumber(Uint(0n)) // 0
Uint.toNumber(Uint(9007199254740991n)) // OK: MAX_SAFE_INTEGER
// Unsafe values throw
Uint.toNumber(Uint(9007199254740992n)) // Error: too large
Uint.toNumber(Uint(10n ** 18n)) // Error: too large
const index = Uint(5n)
const arr = [1, 2, 3, 4, 5, 6]
const value = arr[Uint.toNumber(index)] // 6
function toNumberSafe(uint: BrandedUint256): number | null {
try {
return Uint.toNumber(uint)
} catch {
return null
}
}
Was this page helpful?