# file_size_k

Definition: | string res = file_size_k(atom size, integer width=1)
Description: | Trivial routine to convert a size in bytes to the most appropriate (approximately equivalent) human-readable string, for example 2147483600 ==> "2GB". size: a value in bytes. width: minimum (not maximum) length of result.
pwa/p2js: | Supported (after all it is only a sprintf() wrapper).
Comments: | Sensible rounding occurs: the trailing 00 instead of 48 above was deliberate, and that result of "2GB" is about 10,000 times more accurate than "1.99GB" would have been. Obviously a result of "1000KB" is fine, as would be "1018KB" (deemed preferable to "0.99MB"), whereas 1019K..1023K are rounded up to "1MB". Should you require a different rounding strategy, or more suffixes than KB/MB/GB/TB, you will have to roll your own routine, but obviously you are free to use this as a starting point. Likewise this routine makes no attempt to insert thousand-separator-commas, since pretty much the whole point is to replace them with a suitable suffix (and values above 999TB are deemed to be of limited practial use, or at least very rare or some kind of mistake, anyway). The optional width parameter is intended to simplify alignment, but will not shorten anything that needs more digits, that is, beyond what naturally happens - were you to throw 1e308 at this, you would get a 298-character (in TB) string back, irrespective of any width setting less than 298. This routine can be invoked independently, for instance on the [D_SIZE] elements of the result from dir(), or automatically as part of get_file_size() - note however that the latter can still yield -1 even when asStringK is true.
Implementation: | See builtins\file_utils.e (an autoinclude) for details of the actual implementation.
See Also: | dir, get_file_size