This time, we want render the count of bytes in a human readable way to user. Let's write a program (a full one or a function or other acceptable formats) to do this.
Input
An non-negative integer, in range \$0\leq n<2^{31}\$, which means n bytes.
Output
A string, the human friendly representation of n bytes.
Convention
- If n is less than 1000, add
B
after n and output; otherwise... - Convert to a number less than 1000, plus one of these units: KiB, MiB, GiB; round the number to 3 figures
- For more details of conversion, check out these testcases
Testcases
0 -> 0B
1 -> 1B
42 -> 42B
999 -> 999B
1000 -> 0.98KiB
1024 -> 1.00KiB
2018 -> 1.97KiB
10086 -> 9.85KiB
100010 -> 97.7KiB
456789 -> 446KiB
20080705 -> 19.2MiB
954437177 -> 910MiB
1084587701 -> 1.01GiB
1207959551 -> 1.12GiB
2147483647 -> 2.00GiB
Rules
- This is code-golf, so shortest bytes win
- Standard loopholes forbidden
- You should output exactly the same to the testcase:
- No white space or other symbols between number and units;
- Use
KiB
, notKB
,kib
, orkb
; - Leading or trailing white spaces are optional
0.98KiB
is using 2 significant figures, not 3 – the leading0
doesn't count. \$\endgroup\$