summaryrefslogtreecommitdiff
path: root/scripts/library/size_to_human.sh
blob: 1d13eeb4d918870ff326b9a9d90ef480a3aa5ec4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
size_to_human() {
        awk -v size="$1" '
        BEGIN {
                suffix[1] = "B"
                suffix[2] = "KiB"
                suffix[3] = "MiB"
                suffix[4] = "GiB"
                suffix[5] = "TiB"
                suffix[6] = "PiB"
                suffix[7] = "EiB"
                count = 1

                while (size > 1024) {
                        size /= 1024
                        count++
                }

                sizestr = sprintf("%.2f", size)
                sub(/\.?0+$/, "", sizestr)
                printf("%s %s", sizestr, suffix[count])
        }'
}