Skip to content

Instantly share code, notes, and snippets.

@xentec
Last active January 29, 2016 15:14
Show Gist options
  • Save xentec/d02888591d4b7d21fa01 to your computer and use it in GitHub Desktop.
Save xentec/d02888591d4b7d21fa01 to your computer and use it in GitHub Desktop.
string humanByte(byte decimals = 2)(double bytes)
{
import std.string;
enum suffix = [ "", "K", "M", "G", "T", "P", "E"];
byte i;
while(bytes > 1024.0) {
bytes /= 1024.0;
i++;
}
return format("%."~ decimals.to!string ~"f %siB", bytes, suffix[i]);
}
string humanByte(double bytes, byte decimals = 2)
{
import std.string;
enum suffix = [ "", "K", "M", "G", "T", "P", "E"];
byte i;
while(bytes > 1024.0) {
bytes /= 1024.0;
i++;
}
return format(format("%%.%df %siB", decimals, suffix[i]), bytes);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment