File size: 884 Bytes
a8b3f00 |
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 |
/*
* Formats a number with comma separators.
formatNumber(1234567) will return '1,234,567'
formatNumber(1234567.89) will return '1,234,567.89'
*/
export const formatNumber = (num: number | string) => {
if (!num)
return num
const parts = num.toString().split('.')
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',')
return parts.join('.')
}
export const formatFileSize = (num: number) => {
if (!num)
return num
const units = ['', 'K', 'M', 'G', 'T', 'P']
let index = 0
while (num >= 1024 && index < units.length) {
num = num / 1024
index++
}
return `${num.toFixed(2)}${units[index]}B`
}
export const formatTime = (num: number) => {
if (!num)
return num
const units = ['sec', 'min', 'h']
let index = 0
while (num >= 60 && index < units.length) {
num = num / 60
index++
}
return `${num.toFixed(2)} ${units[index]}`
}
|