mirror of
https://github.com/plausible/analytics.git
synced 2024-12-21 16:41:31 +03:00
25 lines
705 B
JavaScript
25 lines
705 B
JavaScript
|
const THOUSAND = 1000
|
||
|
const HUNDRED_THOUSAND = 100000
|
||
|
const MILLION = 1000000
|
||
|
const HUNDRED_MILLION = 100000000
|
||
|
|
||
|
export default function numberFormatter(num) {
|
||
|
if (num >= THOUSAND && num < MILLION) {
|
||
|
const thousands = num / THOUSAND
|
||
|
if (thousands === Math.floor(thousands) || num >= HUNDRED_THOUSAND) {
|
||
|
return Math.floor(thousands) + 'k'
|
||
|
} else {
|
||
|
return (Math.floor(thousands * 10) / 10) + 'k'
|
||
|
}
|
||
|
} else if (num >= MILLION && num < HUNDRED_MILLION) {
|
||
|
const millions = num / MILLION
|
||
|
if (millions === Math.floor(millions)) {
|
||
|
return Math.floor(millions) + 'm'
|
||
|
} else {
|
||
|
return (Math.floor(millions * 10) / 10) + 'm'
|
||
|
}
|
||
|
} else {
|
||
|
return num
|
||
|
}
|
||
|
}
|