I was refactoring some old code and came across the following line of code to convert bytes to GB.
decimal GB = KB / 1024 / 1024 / 1024;
Is
public static string BytesToString(this long bytes, string format = "#,##0.00") {
var unitstr = new string[] { "B", "KB", "MB", "GB", "TB" };
var bytesd = Convert.ToDouble(bytes);
var unit = 0;
while (bytesd / 1024D > 1 && unit < unitstr.Length) {
unit++; bytesd /= 1024D;
}
return string.Format("{0:" + format + "}{1}", bytesd, unitstr[unit]);
}