Challenge and origin
On Stack Overflow a popular question is: How to convert byte size into human readable format in java? The most up voted answer has a quite nice method for doing this, but this is codegolf and we can do better, can't we?
Your challenge is to write a method or program that coverts the given number of bytes to the correct human readable format and prints the result to the standard out of your language.*
*See the rules for more clarification!
Input
The input will always be a positive number of bytes with a maximum of (2^31)-1.
Output
You may choose if you prefer the International System of Units or the binary notation as output (the SI notation probably saves you some bytes).
SI: B, kB, MB, GB
Binary: B, KiB, MiB, GiB
Note: Higher units than GB or GiB are not posible due to the restricted input range.
Example output
International System of Units:
Input Output
0 0 B
999 999 B
1000 1.0 kB
1023 1.0 kB
1024 1.0 kB
1601 1.6 kB
160581 160.6 kB
4066888 4.1 MB
634000000 634.0 MB
2147483647 2.1 GB
Binary:
Input Output
0 0 B
999 999 B
1000 1000 B
1023 1023 B
1024 1.0 KiB
1601 1.6 KiB
160581 156.8 KiB
4066888 3.9 MiB
634000000 604.6 MiB
2147483647 2.0 GiB
Rules
- Build-in functions for byte formatting are not allowed!
- The output should always be in the same notation standard, you may not mix SI or binary;
- The output should always be in the largest unit possible where the resulting number is still higher or equal to one;
- The output should always have one decimal number, but you may choose to print an integer number when the resulting output is in bytes (B);
- You may choose if you would like to add a space, tab or nothing between the number and the unit;
- Input is received via STDIN or function parameters;
- Output is printed to the console or returned as string (or similar character container);
- This is code golf, so the shortest answer wins. Have fun!