UNPKG

454 BJavaScriptView Raw
1import { isNumeric } from '@typen/num-strict';
2
3class Mag {
4 constructor(digit = 2, sep = 3) {
5 this.dg = digit;
6 this.reg = new RegExp(`\\d(?=(\\d{${sep || 3}})+${digit > 0 ? '\\.' : '$'})`, 'g');
7 }
8
9 static build(digit, sep) {
10 return new Mag(digit, sep);
11 }
12
13 parse(any) {
14 return isNumeric(any) ? this.format(any) : String(any);
15 }
16
17 format(num) {
18 return num.toFixed(this.dg).replace(this.reg, '$&,');
19 }
20
21}
22
23export { Mag };