numberMask
A mask for numbers and amounts: thousands separator, fraction, minus, prefix and suffix.
Signature
ts
function numberMask(options?: NumberMaskOptions): Mask| Option | Default | Description |
|---|---|---|
thousands | ' ' | Thousands separator |
decimal | ',' | Decimal separator. A dot from the numpad is accepted too |
precision | 2 | Digits after the separator; 0 — integers only |
negative | false | Allow a minus |
prefix, suffix | '' | Text before and after the number: '$', ' ₽', ' %' |
The unmasked value is a JavaScript-style string: 1 234,5 → '1234.5'. To get a number use Number(unmask(...)) or coerce.number in a schema. Leading zeros are removed, ,5 becomes 0,5.
Examples
ts
mask('1234567.891', numberMask()); // '1 234 567,89'
mask('1234567.5', numberMask({ thousands: ',', decimal: '.' })); // '1,234,567.5'
mask('-42', numberMask({ negative: true, precision: 0 })); // '-42'
mask('99', numberMask({ prefix: '$', thousands: ',', decimal: '.' })); // '$99'
unmask('1 500,50 ₽', money({ currency: '₽' })); // '1500.50'See also
- Ready-made masks —
money,percent