eg:
200 B ---> 200 B
1024B ---> 1 KB
10343532 B --> 9.86MB
1201759081 B --> 1.12GB
909584 B ---> 888.27 KB
function formatSize(size = 0) {
let unit = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'X']
let idx = 0
let pre = size
let cur = pre / 1024
while (cur > 1) {
pre = cur
idx++
cur /= 1024
}
pre = pre ? pre.toFixed(2) : 0
return `${pre} ${unit[idx]}`
}