Skip to content

numberMask ​

用于数字和金额的掩码:千位分隔符、小数部分、负号、前缀和后缀。

签名 ​

ts
function numberMask(options?: NumberMaskOptions): Mask
选项默认值说明
thousands' '千位分隔符
decimal','小数分隔符。也接受小键盘的句点
precision2小数位数;0 表示只允许整数
negativefalse允许负号
prefix、suffix''数字前后的文本:'$'、' ₽'、' %'

去掉掩码后的值是 JavaScript 格式的字符串:1 234,5 → '1234.5'。如需数字,请使用 Number(unmask(...)) 或在 Schema 中使用 coerce.number。开头多余的零会被去掉,,5 会变为 0,5。

示例 ​

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'

另请参阅 ​