-
Notifications
You must be signed in to change notification settings - Fork 0
/
number.js
76 lines (74 loc) · 1.85 KB
/
number.js
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
/**
* @name formatNumber
* @summary formats number to locale while keeping the specified decimals
*
* @param {Number} value
* @param {Number} decimals (optional) number of decimal places
* @param {String} locale (optional) 2 letter country code
* Default: system default
* @param {String} separator (optional) decimal separator.
* Default: system default
*
* @returns {String}
*/
export const formatNumber = (value, decimals, locale, separator) => {
separator = separator || (1.1).toLocaleString(locale).replace(/[0-9]/g, '')
const int = parseInt(value)
let reminder = Math.abs(value - int)
const reminderX = reminder
if (reminder > 0) {
reminder = separator + `${reminder}`.split('.')[1]
reminder = decimals === 0
? ''
: decimals > 0
? reminder.slice(0, decimals + 1)
: reminder
} else {
reminder = ''
}
return `${int.toLocaleString(locale)}${reminder}`
}
/**
* @name round
* @summary rounds a number to a fixed decimal places and avoids unintentional use of exponents
*
* @param {Number} value
* @param {Number} decimals
*
* @returns {String}
*/
export const round = (value = 0, decimals = 0) => {
value = Number(value)
return Number(Math.round(value + 'e' + decimals) + 'e-' + decimals).toFixed(decimals)
}
/**
* @name shorten
* @summary formats number in short form. Eg: converts `1000` to `"1K"`
*
* @param {Number} value
* @param {Number} decimals Default: `0`
*
* @returns {String}
*/
export const shorten = (value, decimals = 0) => {
let label = ''
let divider = 1
if (value < 1e3) {
divider = 1
label = ''
} else if (value >= 1e9) {
// billion
divider = 1e9
label = 'B'
} else if (value >= 1e6) {
// million
divider = 1e6
label = 'M'
} else if (value >= 1e3) {
// thousand
divider = 1e3
label = 'K'
}
value = round(value / divider, decimals)
return `${value}${label}`
}