-
Notifications
You must be signed in to change notification settings - Fork 3
/
format-number-string.ts
36 lines (28 loc) · 1.07 KB
/
format-number-string.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
export default (numStr: number | string, {
space = ' ', // just space, but nbsp; may also be passed here
delimiter = ',',
minus = '−',
formatThousands = false,
} = {}): string => {
// convert to number
// use + instead of parseFloat, because we prefer to get a NaN when it's not possible to parse a number
const num = +numStr;
if (Number.isNaN(num)) {
throw new Error(`'Non valid number is passed: ${numStr}`);
}
// string→number coercion after toFixed is for removing trailing zeros when they're existed: 17.00 → 17
const numStrFixedDigits = (+num.toFixed(2)).toString();
const uIntPart = numStrFixedDigits.split('.')[0].replace('-', '');
let result = numStrFixedDigits;
if (formatThousands && uIntPart.length > 3 || uIntPart.length > 4) {
const formattedUIntPart = uIntPart
.split('')
.reverse()
.reduce((acc, x, i) => [x, acc].join(i > 0 && i % 3 === 0 ? space : ''), '');
result = result.replace(uIntPart, formattedUIntPart);
}
result = result
.replace('.', delimiter)
.replace('-', minus);
return result;
};