---
title: "Format numbers with JavaScript Intl in C#"
url: https://recursiveai.org/oneecma/cookbook/format-numbers-with-javascript-intl/
description: "Run JavaScript Intl.NumberFormat in C# with OneEcma: format currency, round to two decimal places and read formatting parts, with runnable code."
version: "1.1.1"
---

# Format numbers with JavaScript Intl in C#

**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/format-numbers-with-javascript-intl/format-numbers-with-javascript-intl.zip)

Format currency for a chosen locale inside your C# process.

Supported locales are `en-US`, `en-GB`, `es` (and `es-US`), `fr`, `de` and `pt-BR`; an unsupported locale such as `ja-JP` falls back to the host’s default locale (`en-US` unless changed).

```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 RecursiveAI.OneEcma;
var engine = new ScriptEngine();
Console.WriteLine(engine.Evaluate("""
new Intl.NumberFormat('de', {
  style: 'currency', currency: 'EUR'
}).format(1234.5)
""").AsString);
```

```text
1.234,50 €
```

Unzip [the example](https://recursiveai.org/oneecma/cookbook/format-numbers-with-javascript-intl/format-numbers-with-javascript-intl.zip), then `dotnet run`.

## The full program

```csharp
using RecursiveAI.OneEcma;

var engine = new ScriptEngine();
foreach (string locale in new[] { "en-US", "de" })
{
    var format = engine.Evaluate($$"""
        new Intl.NumberFormat('{{locale}}', {
          style: 'currency', currency: 'EUR'
        }).format(1234.5)
        """);
    Console.WriteLine($"{locale}: {format.AsString}");
}
Console.WriteLine(engine.Evaluate("""
    new Intl.NumberFormat('en-US', {
      minimumFractionDigits: 2, maximumFractionDigits: 2,
      roundingMode: 'halfEven'
    }).format('1.225')
    """).AsString);
```

`dotnet run` prints:

```text
en-US: €1,234.50
de: 1.234,50 €
1.22
```

The program compares the same euro amount in two locales, then rounds a decimal string using “half even”: a tie goes to the even last digit. Invalid formatting options throw `JsException` carrying the JavaScript error.

## Variations

Read currency, integer and fraction parts separately instead of splitting the formatted text:

```csharp
using RecursiveAI.OneEcma;
var engine = new ScriptEngine();
Console.WriteLine(engine.Evaluate("""
JSON.stringify(
  new Intl.NumberFormat('en-US', {
    style: 'currency', currency: 'USD'
  }).formatToParts(12.5)
)
""").AsString);
```

```text
[{"type":"currency","value":"$"},{"type":"integer","value":"12"},{"type":"decimal","value":"."},{"type":"fraction","value":"50"}]
```

## Related recipes

- [Call a JavaScript function](https://recursiveai.org/oneecma/cookbook/call-javascript-function/)
- [Exchange BigInt with .NET](https://recursiveai.org/oneecma/cookbook/exchange-bigint-with-dotnet/)

## The short answer

Evaluate `new Intl.NumberFormat(locale, options).format(value)` with OneEcma, then read the returned `JsValue.AsString`. Use an explicit locale and currency rather than relying on the computer’s regional settings. The snippet at the top formats euros in German; the full program compares German and US English output and shows decimal rounding.

OneEcma’s [Intl documentation](https://recursiveai.org/oneecma/docs/intl/) says formatting data comes from embedded CLDR tables. Its supported locales are `en-US`, `en-GB`, `es` (and `es-US`), `fr`, `de` and `pt-BR`, with Latin digits. Choose this approach when you need to run JavaScript formatting logic in .NET with that locale set, not when you need every locale available in a browser.

## What is Intl.NumberFormat?

[`Intl.NumberFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat) is JavaScript’s locale-aware number formatter. It turns a numeric value into display text. The locale controls separators and placement; options select decimal, currency or other styles. Formatting does not change the stored numeric value.

`ScriptEngine.Evaluate` executes the JavaScript expression and returns its string result. There is no browser page to initialize and no DOM involved. If your application already supplies a JavaScript expression for displaying amounts, you can keep that expression and use its result in a .NET UI or report.

## How can I format numbers as currency in JavaScript?

Set `style` to `currency` and supply a currency code such as `EUR`. The full program uses exactly the same amount and currency in both iterations. Only the locale changes: the English output places the euro symbol before the amount, while the German output places it after the amount and uses a comma for the decimal separator.

Currency formatting is not currency conversion. The code never fetches an exchange rate or changes euros into dollars. Choose the monetary unit before calling the formatter. Store numeric amounts separately from the resulting display string, so another calculation does not have to parse punctuation or currency symbols back out of it.

## How can I format numbers to 2 decimal places in JavaScript?

Set both `minimumFractionDigits` and `maximumFractionDigits` to `2`, as the full program does. The minimum keeps trailing zeroes; the maximum controls rounding. Set `roundingMode` explicitly when ties matter. In this example, `halfEven` turns the exact decimal string `1.225` into `1.22`.

The library documents that numeric strings and BigInts retain their digits during formatting, while a double is first converted to its shortest round-trip decimal. Passing a decimal string is therefore useful when the input already exists as exact decimal text. It does not recover digits that were lost earlier by converting the input to a JavaScript Number.

## How do I format numbers in JavaScript without splitting the result?

Use `formatToParts`, as in the variation. It returns typed pieces for the currency, integer, decimal separator and fraction. The variation serializes the parts only to make them visible in console output; your JavaScript can instead inspect each part’s `type` and `value`.

Use these parts when styling the currency symbol separately or placing the fractional digits in a different element. Avoid splitting on a period or comma: the full program shows why that assumption fails across locales. Keep literal parts too, including spaces, because they belong to the locale’s formatting pattern.
