Compact Format
Convert large numbers to readable K/M/B/T abbreviations.
Basic Usage
javascript
import { calc } from "a-calc";
calc("1234 | !c"); // '1.23K'
calc("1234567 | !c"); // '1.23M'
calc("1234567890 | !c"); // '1.23B'
calc("1234567890000 | !c"); // '1.23T'Default Behavior
Compact format uses truncation (not rounding) to avoid overstating numbers.
javascript
calc("999999 | !c"); // '999.99K' (not 1M)
calc("1999 | !c =0"); // '1K' (not 2K)Decimal Places
javascript
calc("1234567 | !c"); // '1.23M' (default 2)
calc("1234567 | !c =1"); // '1.2M'
calc("1234567 | !c =0"); // '1M'
calc("1234567 | !c =4"); // '1.2345M'Built-in Presets
| Preset | Units | Step | Example |
|---|---|---|---|
en | ["", "K", "M", "B", "T"] | 1000 | 1.23M |
cn | ["", "千", "百万", "十亿", "万亿"] | 1000 | 1.23百万 |
wan | ["", "万", "亿", "万亿"] | 10000 | 1.23万 |
storage | ["B", "KB", "MB", "GB", "TB"] | 1024 | 1MB |
indian | ["", "K", "L", "Cr"] | 1000/100 | 12.34L |
javascript
calc("12345 | !c:wan"); // '1.23万'
calc("123456789 | !c:wan"); // '1.23亿'
calc("1048576 | !c:storage"); // '1MB'
calc("123456 | !c:indian"); // '1.23L'Custom Presets
javascript
import { set_config, calc } from "a-calc";
set_config({
_compact: {
rmb: { symbols: ["", "万", "亿"], step: 10000 },
},
});
calc("12345 | !c:rmb"); // '1.23万'Array Step (Irregular Progression)
javascript
// Time units: seconds → minutes → hours → days
set_config({
_compact: {
time: {
symbols: ["s", "m", "h", "d"],
step: [60, 60, 24],
},
},
});
calc("3600 | !c:time"); // '1h'
calc("86400 | !c:time"); // '1d'Combining with Unit Syntax
javascript
calc("1234567 | !c !ua:shares"); // '1.23M shares'
calc("1234567 | !c !ub:$"); // '$1.23M'
calc("1234567 | +!c !um:$"); // '+$1.23M'
calc("12345 | !c:wan !ua:元"); // '1.23万元'Sign Display
javascript
calc("1234567 | +!c"); // '+1.23M'
calc("-1234567 | +!c"); // '-1.23M'Edge Cases
javascript
calc("999 | !c"); // '999' (below threshold)
calc("0 | !c"); // '0'
calc("1e18 | !c"); // '1000000T' (beyond T)Default Compact Preset
javascript
set_config({ _compact_default: "wan" });
calc("12345 | !c"); // '1.23万'