Formatting
Use | to separate the expression from formatting rules.
Decimal Places
javascript
calc("10 / 3 | =2"); // '3.33' exactly 2 places
calc("1.5 | =3"); // '1.500' pad with zeros
calc("10 / 3 | <=2"); // '3.33' at most 2 places, no paddingRounding Modes
Default is truncation. Other modes are available:
javascript
calc("1.129 | =2"); // '1.12' truncate (default)
calc("1.125 | ~5=2"); // '1.13' round half up
calc("1.125 | ~6=2"); // '1.12' banker's rounding (common in finance)Thousands Separator
javascript
calc("1234567 | ,"); // '1,234,567'
calc("1234567.89 | =2,"); // '1,234,567.89'Percentage
javascript
calc("0.1234 | %"); // '12.34%'
calc("0.1234 | %=2"); // '12.34%'Show Positive Sign
javascript
calc("100 | +"); // '+100'
calc("-100 | +"); // '-100'Return Number Type
javascript
calc("1 + 2"); // '3' (string)
calc("1 + 2 | !n"); // 3 (number)Combining Rules
javascript
calc("1234567.126 | ~5=2,+"); // '+1,234,567.13'
calc("0.12345 | ~5%=2"); // '12.35%'Quick Reference
| Rule | Description | Example |
|---|---|---|
=N | Exactly N decimal places | =2 → 1.50 |
<=N | At most N decimal places | <=2 → 1.5 |
~5 | Round half up | 1.125 → 1.13 |
~6 | Banker's rounding | 1.125 → 1.12 |
, | Thousands separator | 1234567 → 1,234,567 |
% | Percentage | 0.12 → 12% |
+ | Show positive sign | 100 → +100 |
!n | Return number type | '3' → 3 |
The fmt Function
Use fmt for pure formatting — it's more efficient:
javascript
import { fmt } from "a-calc";
fmt(1234567, ",=2"); // '1,234,567.00'
fmt(0.1234, "%=2"); // '12.34%'Next Steps
- Thousands Presets — EU, Indian, and other regional formats
- Compact Format — K, M, 万, 亿 abbreviations
- API Reference — full formatting options