Skip to content

Instantly share code, notes, and snippets.

@InvaderZim85
Created April 12, 2019 16:09
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/9f6dbda7d976f959f6c6d6249f13779b to your computer and use it in GitHub Desktop.
Save InvaderZim85/9f6dbda7d976f959f6c6d6249f13779b to your computer and use it in GitHub Desktop.
public string ConvertFileSize(long size)
{
switch (size)
{
case var _ when size < 1024:
return $"{size} Bytes";
case var _ when size >= 1024 && size < Math.Pow(1024, 2):
return $"{size / 1024:N2} KB";
case var _ when size >= Math.Pow(1024, 2) && size < Math.Pow(1024, 3):
return $"{size / Math.Pow(1024, 2):N2} MB";
case var _ when size >= Math.Pow(1024, 3):
return $"{size / 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