---
title: "Exchange JavaScript BigInt with C# BigInteger"
url: https://recursiveai.org/oneecma/cookbook/exchange-bigint-with-dotnet/
description: "Pass System.Numerics.BigInteger to JavaScript BigInt and back with OneEcma. Preserve exact integer digits without a double conversion."
version: "1.1.1"
---

# Exchange JavaScript BigInt with C# BigInteger

**OneEcma** is a free, open-source JavaScript interpreter for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneEcma) · [GitHub](https://github.com/recursive-ai-oss/oneecma-csharp) · [Docs](https://recursiveai.org/oneecma/docs/) · [Download example (.zip)](https://recursiveai.org/oneecma/cookbook/exchange-bigint-with-dotnet/exchange-bigint-with-dotnet.zip)

```shell
dotnet add package RecursiveAI.OneEcma
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using System.Numerics;
using RecursiveAI.OneEcma;
var engine = new ScriptEngine();
var twice = engine.Evaluate("n => n * 2n");
var input = BigInteger.Parse(
    "9007199254740993");
var result = engine.Call(twice, default,
    [JsValue.FromBigInt(input)]);
Console.WriteLine(result.AsBigInt);
```

```text
18014398509481986
```

In the snippet at the top, `default` is `JsValue.Undefined`, the JavaScript `this` argument.

Unzip [the example](https://recursiveai.org/oneecma/cookbook/exchange-bigint-with-dotnet/exchange-bigint-with-dotnet.zip), then `dotnet run`.

## The full program

```csharp
using System.Numerics;
using RecursiveAI.OneEcma;

var engine = new ScriptEngine();
var addOne = engine.Evaluate("n => n + 1n");
BigInteger input = BigInteger.Parse("123456789012345678901234567890");
JsValue result = engine.Call(addOne, JsValue.Undefined,
    [JsValue.FromBigInt(input)]);
BigInteger output = result.AsBigInt;
Console.WriteLine($"input: {input}");
Console.WriteLine($"output: {output}");
Console.WriteLine($"exact: {output == input + BigInteger.One}");
```

`dotnet run` prints:

```text
input: 123456789012345678901234567890
output: 123456789012345678901234567891
exact: True
```

`FromBigInt` sends the .NET integer; `AsBigInt` reads the JavaScript integer back. The full program checks that adding one preserved every digit. Mixing BigInt and Number in JavaScript arithmetic throws `JsException` carrying a JavaScript `TypeError`.

## Variations

For a JSON payload, explicitly encode BigInt values as decimal strings:

```csharp
using RecursiveAI.OneEcma;
var engine = new ScriptEngine();
Console.WriteLine(engine.Evaluate("""
JSON.stringify({ id: 9007199254740993n },
  (key, value) => typeof value === 'bigint'
    ? value.toString() : value)
""").AsString);
```

```text
{"id":"9007199254740993"}
```

Compare an exact BigInt with its Number conversion, then show integer division:

```csharp
using RecursiveAI.OneEcma;
var engine = new ScriptEngine();
Console.WriteLine(engine.Evaluate("""
[9007199254740993n.toString(),
 String(Number(9007199254740993n)),
 (7n / 2n).toString()].join('\n')
""").AsString);
```

```text
9007199254740993
9007199254740992
3
```

## Related recipes

- [Call a JavaScript function](https://recursiveai.org/oneecma/cookbook/call-javascript-function/)
- [Exchange JavaScript typed arrays](https://recursiveai.org/oneecma/cookbook/exchange-javascript-typed-arrays/)

## The short answer

Use `JsValue.FromBigInt(BigInteger)` when passing a .NET integer to JavaScript, and read `JsValue.AsBigInt` on the result. Neither step needs a `double`. The snippet at the top sends an integer beyond JavaScript Number’s safe integer range, doubles it in JavaScript and reads the exact integer back in C#.

The [public host API](https://recursiveai.org/oneecma/docs/host-api/) exposes these operations directly. You do not need generated bindings for this low-level call. Keep the function source separate from the value: evaluate the function once, then supply the integer in the argument list rather than interpolating decimal digits into JavaScript source.

## What is the C# equivalent of JavaScript BigInt?

`System.Numerics.BigInteger` is the .NET type used at this boundary. Both represent integers that do not fit within fixed-width integer types. The JavaScript side uses BigInt literals with an `n` suffix, such as the `1n` in the full program. The C# side parses the decimal input into a `BigInteger` before constructing a `JsValue`.

The return path is equally explicit. `engine.Call` returns a JavaScript value; reading `AsBigInt` produces the .NET integer. For a function you do not control, inspect `IsBigInt` before choosing that accessor. A Number, a decimal string and a BigInt are different JavaScript value kinds even when they display the same digits.

## What is the difference between JavaScript BigInt and Number?

[JavaScript BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) represents integers, while Number uses floating-point representation. Number’s maximum safe integer is `9007199254740991`. Above that, not every adjacent integer is distinguishable. The second variation demonstrates the difference: the BigInt text is `9007199254740993`, but explicitly converting it to Number produces `9007199254740992`.

Use BigInt for exact integer IDs, counters and integer calculations. It is not a decimal-money type: it does not store a fractional component. Keep both operands as BigInt during arithmetic; the full program adds `1n`, not the Number literal `1`.

## How does JavaScript BigInt division work?

BigInt division truncates toward zero, as documented by MDN. The second variation prints `3` for `7n / 2n`, not `3.5`. Reading the result into a C# `BigInteger` preserves that result; it cannot restore a fractional part that the JavaScript operation discarded.

Decide whether the task is integer arithmetic before crossing the boundary. If a calculation needs fractions, returning a BigInt is the wrong contract unless you have explicitly chosen a scale and carry that scale separately. For the full program’s exact increment operation, integer arithmetic is the intended behavior.

## How do I serialize JavaScript BigInt to JSON?

The first variation supplies a `JSON.stringify` replacer that turns BigInt values into decimal strings. The resulting payload contains an ordinary quoted string for `id`. MDN documents that plain JSON serialization does not support BigInt by default; make the conversion policy explicit rather than relying on an automatic numeric conversion.

This JSON representation is a separate boundary from `FromBigInt` and `AsBigInt`. For in-process calls, use the typed API and avoid serialization. For a text payload, document which fields are integer strings so the receiver can parse those fields deliberately. Do not convert every digit-looking string into an integer: account codes and other text identifiers may have meaningful leading zeroes.
