Thousands Formatting
a-calc provides flexible thousands formatting covering global number format standards.
Built-in Presets
| Preset | Example | Region |
|---|---|---|
en | 1,234,567.89 | US, UK, China |
eu | 1.234.567,89 | Germany, Italy, Spain |
swiss | 1'234'567.89 | Switzerland |
space | 1 234 567.89 | ISO standard, Nordic |
fr | 1 234 567,89 | France (non-breaking space) |
indian | 12,34,567.89 | India |
wan | 1,2345,678.89 | China/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
| Option | Default | Description |
|---|---|---|
sep | ',' | Thousands separator |
point | '.' | Decimal point |
grouping | [3] | Grouping rule |
min_len | 0 | Min digits to trigger (0 = always) |
point_group | false | Group decimal part too |
fn | null | Custom 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€'