Skip to content

Instantly share code, notes, and snippets.

@InvaderZim85
Last active April 12, 2019 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save InvaderZim85/a0b6a7d15d61f0a807b6029e4ba64c01 to your computer and use it in GitHub Desktop.
Save InvaderZim85/a0b6a7d15d61f0a807b6029e4ba64c01 to your computer and use it in GitHub Desktop.
public string ConvertFileSize(long size)
{
switch (size)
{
case long n when (n < 1024):
return $"{n} Bytes";
case long n when (n >= 1024 && n < Math.Pow(1024, 2)):
return $"{n / 1024:N2} KB";
case long n when (n >= Math.Pow(1024, 2) && n < Math.Pow(1024, 3)):
return $"{n / Math.Pow(1024, 2):N2} MB";
case long n when (n >= Math.Pow(1024, 3)):
return $"{n / Math.Pow(1024, 3):N2} GB";
default:
return size.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment