Skip to content

Thousands Formatting

a-calc provides flexible thousands formatting covering global number format standards.

Built-in Presets

PresetExampleRegion
en1,234,567.89US, UK, China
eu1.234.567,89Germany, Italy, Spain
swiss1'234'567.89Switzerland
space1 234 567.89ISO standard, Nordic
fr1 234 567,89France (non-breaking space)
indian12,34,567.89India
wan1,2345,678.89China/Japan (wan-based)

Usage

javascript
import { calc } from "a-calc";

calc("1234567.89 | !t:en"); // '1,234,567.89'
calc("1234567.89 | !t:eu"); // '1.234.567,89'
calc("1234567.89 | !t:swiss"); // "1'234'567.89"
calc("1234567.89 | !t:indian"); // '12,34,567.89'
calc("1234567.89 | !t:wan"); // '1,2345,678.89'

Custom Presets

javascript
import { calc, set_config } from "a-calc";

set_config({
  _thousands: {
    btc: { sep: " ", point: ".", grouping: [4] },
  },
});

calc("12345678 | !t:btc"); // '1234 5678'

Atomic Config Options

OptionDefaultDescription
sep','Thousands separator
point'.'Decimal point
grouping[3]Grouping rule
min_len0Min digits to trigger (0 = always)
point_groupfalseGroup decimal part too
fnnullCustom formatting function

Grouping Rules

javascript
grouping: [3]; // 1,234,567 (standard)
grouping: [3, 2]; // 12,34,567 (Indian)
grouping: [4]; // 1,2345,6789 (wan-based)

The last value in the array repeats for all remaining groups.

Custom Function

javascript
set_config({
  _thousands: {
    custom: {
      fn: (numStr, { intPart, decPart, sign }) => {
        const formatted = intPart.replace(/\B(?=(\d{4})+(?!\d))/g, "_");
        return sign + formatted + (decPart ? "." + decPart : "");
      },
    },
  },
});
// 1234567.89 → "123_4567.89"

When fn is present, all other atomic options are ignored.

Combining with Formatting

javascript
calc("1234567.126 | =2 !t:eu"); // '1.234.567,12'
calc("1234567.89 | + !t:en"); // '+1,234,567.89'
calc("1234567.89 | !t:eu !ua:€"); // '1.234.567,89€'

Default Preset

javascript
import { set_config } from "a-calc";

set_config({ _thousands_default: "eu" });
calc("1234567.89 | =2,"); // '1.234.567,89'

Unit Association

javascript
set_config({
  _unit_thousands_map: {
    $: "en",
    "€": "eu",
    "¥": "wan",
  },
});

calc("1234567 | !ua:€"); // '1.234.567€'

Released under the MIT License