6
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
@tsh "Use KiB, not KB" - are you sure about the 1000 then? The SI units are 1kB=1000B and 1KiB=1024B. – ngn – 2018-07-07T08:30:48.293
@ngn Yes. 1000 should be converted to 0.98KiB, not 1000B, or 1kB. And there are always at most 3 significant figures, not 4 (for 1000B). – tsh – 2018-07-07T08:33:10.187
3I think
0.98KiB
is using 2 significant figures, not 3 – the leading0
doesn't count. – O.O.Balance – 2018-07-07T08:36:41.190@O.O.Balance Ah, you are right. I would change the description some how (testcases would be unchanged); So 3 <del>significant</del> figures now. – tsh – 2018-07-07T08:39:59.390
Why not leave it significant figures and change the one testcase? Significant figures is a standard way of formatting numbers (and I already have a working program :P). – O.O.Balance – 2018-07-07T08:49:00.220
@user202729 Because there isn't 0.08bits. – tsh – 2018-07-07T10:02:49.797
1@user202729 The 3 figures rule seems to apply only if $n\ge1000$. – Erik the Outgolfer – 2018-07-07T11:00:04.787
How about
1023079
, should the output be999KiB
or0.98MiB
? – None – 2018-07-07T12:07:30.867(How exactly should the numbers be rounded? Half-up?) – user202729 – 2018-07-07T12:28:17.060
Besides
1023079
, another interesting test case is1023488
. – None – 2018-07-07T12:39:23.8507I remember seeing this question in the Sandbox, though I don't know how long it was there - either it should have been there longer, or more people need to visit the Sandbox and leave comments there. – sundar - Reinstate Monica – 2018-07-07T12:44:42.383
So, the rule of the 3 digits does not apply for n < 1000, does it? – PieCot – 2018-07-07T15:12:47.833
@user202729 "3 figures" rule only apply to n >= 1000. – tsh – 2018-07-07T15:55:33.060
@PieCot "3 figures" rule only apply to n >= 1000. – tsh – 2018-07-07T15:55:55.207
@YiminRong I didn't even noticed such a testcase. Since it's too late to change anything. I would say both are OK. – tsh – 2018-07-07T15:57:16.940
@sundar Obviously the latter. – user202729 – 2018-07-07T15:59:54.137