This repository was archived by the owner on Sep 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatNumber.js
More file actions
98 lines (96 loc) · 2.77 KB
/
Copy pathformatNumber.js
File metadata and controls
98 lines (96 loc) · 2.77 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import numeral from 'numeral';
/**
* special number formatting that can deal with microtypography
* and "prepend currencies" (e.g., −$1234.57)
*
* Use {@link initNumeralLocale} to set a custom locale.
*
* @exports formatNumber
* @kind function
*
* @param {number} value - the number to format
* @param {object} options - options, see below
* @param {string} options.format - numeral.js compatible number format
* @param {string} options.prepend - string to prepend to number
* @param {string} options.append - string to append to number
* @param {string} options.minusChar - custom character to use for minus
* @param {number} options.multiply - multiply number before applying format
*
* @example
* // returns '1234.57'
* formatNumber(1234.567)
*
* @example
* // returns '−$1234.57'
* formatNumber(-1234.567, { prepend: '$' })
*
* @export
* @returns {string} - the formatted number
*/
export default function formatNumber(value, options) {
options = {
format: '0.[00]',
prepend: '',
append: '',
minusChar: '−',
plusMinusChar: '±',
multiply: 1,
...options
};
if (value === undefined || isNaN(value) || value === '' || value === null) {
return '';
}
const { format, append, prepend, minusChar, plusMinusChar, multiply } = options;
if (format.includes('%') && Number.isFinite(value)) {
// numeraljs will multiply percentages with 100
// which we don't want to happen
value *= 0.01;
}
value *= multiply;
const parenthesesFormat = format.indexOf('(') > -1;
const fmt = numeral(parenthesesFormat ? value : Math.abs(value)).format(format);
if (
prepend &&
!parenthesesFormat &&
value < 0 &&
currencies.has(prepend.trim().toLowerCase())
) {
// pull minus sign to front
return `${minusChar}${prepend}${fmt.replace('+', '')}${append}`;
} else if (
prepend &&
value >= 0 &&
currencies.has(prepend.trim().toLowerCase()) &&
format.includes('+')
) {
// pull plus sign to front
return `${value === 0 ? plusMinusChar : '+'}${prepend}${fmt.replace('+', '')}${append}`;
} else if (value === 0 && format.includes('+')) {
return `${prepend}${fmt.replace('+', plusMinusChar)}${append}`;
}
if (value < 0 && !parenthesesFormat) {
return `${prepend}${minusChar}${fmt.replace('+', '')}${append}`;
}
return `${prepend}${fmt}${append}`;
}
/*
* list of currency signs that sometimes preceed the value
* @todo: extend this list if users requesting it :)
*/
const currencies = new Set([
'฿',
'₿',
'¢',
'$',
'€',
'eur',
'£',
'gbp',
'¥',
'yen',
'usd',
'cad',
'us$',
'ca$',
'can$'
]);