Skip to content

Instantly share code, notes, and snippets.

@jbnv
Created October 2, 2015 14:40
Show Gist options
  • Select an option

  • Save jbnv/130e014cfa249d71dff3 to your computer and use it in GitHub Desktop.

Select an option

Save jbnv/130e014cfa249d71dff3 to your computer and use it in GitHub Desktop.
Simple C# class for converting an integer into a file size with suffix.
public class FileSize
{
long _value;
long _reduced;
string _units;
public long Reduced { get { return _reduced; } }
public string Units { get { return _units; } }
public FileSize(long value)
{
_value = value;
var units = new string[] { "B", "KB", "MB", "GB" };
var sizeUnitIndex = 0;
_reduced = value;
while (_reduced > 1024)
{
_reduced /= 1024;
sizeUnitIndex++;
}
_units = units[sizeUnitIndex];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment