> ## Documentation Index
> Fetch the complete documentation index at: https://voltaire.tevm.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# Uint.fromNumber

> Create Uint256 from JavaScript number

<Card title="Try it Live" icon="play" href="https://playground.tevm.sh?example=primitives/uint.ts">
  Run Uint examples in the interactive playground
</Card>

<Tabs />

## Number Limitations

JavaScript numbers are **64-bit floating point** (IEEE 754 double precision):

<Warning title="Safe Integer Range">
  Only use numbers within safe integer range: -2^53 + 1 to 2^53 - 1

  For larger values, use **BigInt** instead.
</Warning>

### Safe Integer Range

```typescript theme={null}
// Maximum safe integer
Number.MAX_SAFE_INTEGER  // 9007199254740991 (2^53 - 1)

// Safe values
Uint(0)                        // OK
Uint(1000000)                  // OK
Uint(Number.MAX_SAFE_INTEGER)  // OK

// Unsafe values (loss of precision)
Uint(Number.MAX_SAFE_INTEGER + 1)  // Unreliable
Uint(10 ** 18)                     // Unreliable (exceeds safe range)
```

### Use BigInt for Large Values

```typescript theme={null}
// Don't do this (unsafe)
const largeNumber = 1000000000000000000  // 10^18
Uint(largeNumber)  // May lose precision!

// Do this instead (safe)
const largeBigInt = 1000000000000000000n
Uint.fromBigInt(largeBigInt)  // Accurate

// Or use string
Uint("1000000000000000000")  // Accurate
```

## Validation

### Integer Check

```typescript theme={null}
// Must be integer
Uint(100)    // OK
Uint(0)      // OK

// Decimal values throw
Uint(3.14)   // Error: Must be integer
Uint(1.5)    // Error: Must be integer
Uint(100.1)  // Error: Must be integer
```

### Special Values

```typescript theme={null}
// Invalid numbers throw
Uint(NaN)       // Error: Invalid value
Uint(Infinity)  // Error: Invalid value
Uint(-Infinity) // Error: Invalid value
```

### Negative Values

```typescript theme={null}
// Must be non-negative
Uint(0)    // OK
Uint(1)    // OK

Uint(-1)   // Error: Cannot be negative
Uint(-100) // Error: Cannot be negative
```

## Usage Patterns

### Small Constants

```typescript theme={null}
// Good for small constants
const ZERO = Uint(0)
const ONE = Uint(1)
const TEN = Uint(10)
const HUNDRED = Uint(100)
```

### Array Indices

```typescript theme={null}
function getElement(index: number): BrandedUint256 {
  return Uint(index)
}

// Converting array index to Uint
const items = [1, 2, 3, 4, 5]
items.forEach((_, index) => {
  const indexAsUint = Uint(index)
  // ...
})
```

### Type Conversion

```typescript theme={null}
// When you have number but need Uint
function processNumber(n: number): void {
  if (n < 0 || !Number.isInteger(n)) {
    throw new Error("Invalid number")
  }

  const uint = Uint(n)
  // Use uint...
}
```

## Best Practices

### Prefer BigInt for Ethereum Values

```typescript theme={null}
// Bad: Loses precision
const weiAmount = Uint(1000000000000000000)  // 10^18

// Good: Accurate
const weiAmount = Uint.fromBigInt(1000000000000000000n)

// Good: From string
const weiAmount = Uint("1000000000000000000")
```

### Check Safe Integer Range

```typescript theme={null}
function safeFromNumber(n: number): BrandedUint256 {
  if (!Number.isSafeInteger(n)) {
    throw new Error("Number exceeds safe integer range - use BigInt")
  }
  if (n < 0) {
    throw new Error("Number must be non-negative")
  }
  return Uint(n)
}
```

## See Also

* [from](/primitives/uint256/from) - Universal constructor
* [fromBigInt](/primitives/uint256/from-bigint) - From BigInt (for large values)
* [toNumber](/primitives/uint256/to-number) - Convert to number
