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]);
}
I developed this method here, works up to TB.
private static string FormatBytes(long bytes)
{
string[] Suffix = { "B", "KB", "MB", "GB", "TB" };
int i;
double dblSByte = bytes;
for (i = 0; i < Suffix.Length && bytes >= 1024; i++, bytes /= 1024)
{
dblSByte = bytes / 1024.0;
}
return String.Format("{0:0.##} {1}", dblSByte, Suffix[i]);
}
This is a little improvement of the good JLopez's answer. Here you can choose to have or not the units indication and the kilo unit is written with the lowercase "k" (the uppercase one is for Kelvin)
//note: this is the JLopez answer!!
/// <summary>
/// Return size in human readable form
/// </summary>
/// <param name="bytes">Size in bytes</param>
/// <param name="useUnit ">Includes measure unit (default: false)</param>
/// <returns>Readable value</returns>
public static string FormatBytes(long bytes, bool useUnit = false)
{
string[] Suffix = { " B", " kB", " MB", " GB", " TB" };
double dblSByte = bytes;
int i;
for (i = 0; i < Suffix.Length && bytes >= 1024; i++, bytes /= 1024)
{
dblSByte = bytes / 1024.0;
}
return $"{dblSByte:0.##}{(useUnit ? Suffix[i] : null)}";
}
#region AutoFileSize
public string AutoFileSize(long number)
{
double tmp = number;
string suffix = " B ";
if (tmp > 1024) { tmp = tmp / 1024; suffix = " KB"; }
if (tmp > 1024) { tmp = tmp / 1024; suffix = " MB"; }
if (tmp > 1024) { tmp = tmp / 1024; suffix = " GB"; }
if (tmp > 1024) { tmp = tmp / 1024; suffix = " TB"; }
return tmp.ToString("n") + suffix;
}
#endregion
long number = (long)fu.PostedFile.ContentLength;
To make sure that the compiler pre-calculates the divisors:
decimal GB = KB / (1024 * 1024);
Note that you are actually calculating GiB (gibibyte), not GB (gigabyte). If you really want to calculate GB, that would be:
decimal GB = KB / (1000 * 1000);
I wrote a small utility class that performs conversions between units, hth..
#region StorageDifferential
/// <summary>
/// Converts between Base 2 or Base 10 storage units [TB, GB, MB, KB, Bytes]
/// </summary>
public enum Differential : int
{
/// <summary>
/// Convert Bytes to Kilobytes
/// </summary>
ByteToKilo,
/// <summary>
/// Convert Bytes to Megabytes
/// </summary>
ByteToMega,
/// <summary>
/// Convert Bytes to Gigabytes
/// </summary>
ByteToGiga,
/// <summary>
/// Convert Bytes to Teraytes
/// </summary>
ByteToTera,
/// <summary>
/// Convert Kilobytes to Bytes
/// </summary>
KiloToByte,
/// <summary>
/// Convert Kilobytes to Megabytes
/// </summary>
KiloToMega,
/// <summary>
/// Convert Kilobytes to Gigabytes
/// </summary>
KiloToGiga,
/// <summary>
/// Convert Kilobytes to Terabytes
/// </summary>
KiloToTera,
/// <summary>
/// Convert Megabytes to Bytes
/// </summary>
MegaToByte,
/// <summary>
/// Convert Megabytes to Kilobytes
/// </summary>
MegaToKilo,
/// <summary>
/// Convert Megabytes to Gigabytes
/// </summary>
MegaToGiga,
/// <summary>
/// Convert Megabytes to Terabytes
/// </summary>
MegaToTera,
/// <summary>
/// Convert Gigabytes to Bytes
/// </summary>
GigaToByte,
/// <summary>
/// Convert Gigabytes to Kilobytes
/// </summary>
GigaToKilo,
/// <summary>
/// Convert Gigabytes to Megabytes
/// </summary>
GigaToMega,
/// <summary>
/// Convert Gigabytes to Terabytes
/// </summary>
GigaToTerra,
/// <summary>
/// Convert Terabyte to Bytes
/// </summary>
TeraToByte,
/// <summary>
/// Convert Terabyte to Kilobytes
/// </summary>
TeraToKilo,
/// <summary>
/// Convert Terabytes to Megabytes
/// </summary>
TeraToMega,
/// <summary>
/// Convert Terabytes to Gigabytes
/// </summary>
TeraToGiga,
}
#endregion
#region Storage Sizes
/// <summary>
/// Enumeration of recognized storage sizes [in Bytes]
/// </summary>
public enum StorageSizes : long
{
/// <summary>
/// Base 10 Conversion
/// </summary>
KILOBYTE = 1000,
MEGABYTE = 1000000,
GIGABYTE = 1000000000,
TERABYTE = 1000000000000,
/// <summary>
/// Base 2 Conversion
/// </summary>
KIBIBYTE = 1024,
MEBIBYTE = 1048576,
GIBIBYTE = 1073741824,
TEBIBYTE = 1099511627776,
}
#endregion
#region StorageBase
/// <summary>
/// Storage powers 10 based or 1024 based
/// </summary>
public enum StorageBase : int
{
/// <summary>
/// 1024 Base power, Typically used in memory measurements
/// </summary>
BASE2,
/// <summary>
/// 10 Base power, Used in storage mediums like harddrives
/// </summary>
BASE10,
}
#endregion
/// <summary>
/// Convert between base 1024 storage units [TB, GB, MB, KB, Byte]
/// </summary>
public static class StorageConverter
{
/// <summary>
/// Convert between base 1024 storage units [TB, GB, MB, KB, Byte]
/// </summary>
/// <param name="SizeDifferential">Storage conversion differential [enum]</param>
/// <param name="UnitSize">Size as mutiple of unit type units [double]</param>
/// <param name="BaseUnit">Size of the base power [enum]</param>
/// <returns>Converted unit size [double]</returns>
public static double Convert(Differential SizeDifferential, double UnitSize, StorageBase BaseUnit = StorageBase.BASE10)
{
if (UnitSize < 0.000000000001) return 0;
double POWER1 = 1000;
double POWER2 = 1000000;
double POWER3 = 1000000000;
double POWER4 = 1000000000000;
if (BaseUnit == StorageBase.BASE2)
{
POWER1 = 1024;
POWER2 = 1048576;
POWER3 = 1073741824;
POWER4 = 1099511627776;
}
switch (SizeDifferential)
{
case Differential.ByteToKilo:
return UnitSize / POWER1;
case Differential.ByteToMega:
return UnitSize / POWER2;
case Differential.ByteToGiga:
return UnitSize / POWER3;
case Differential.ByteToTera:
return UnitSize / POWER4;
case Differential.KiloToByte:
return UnitSize * POWER1;
case Differential.KiloToMega:
return UnitSize / POWER1;
case Differential.KiloToGiga:
return UnitSize / POWER2;
case Differential.KiloToTera:
return UnitSize / POWER3;
case Differential.MegaToByte:
return UnitSize * POWER2;
case Differential.MegaToKilo:
return UnitSize * POWER1;
case Differential.MegaToGiga:
return UnitSize / POWER1;
case Differential.MegaToTera:
return UnitSize / POWER2;
case Differential.GigaToByte:
return UnitSize * POWER3;
case Differential.GigaToKilo:
return UnitSize * POWER2;
case Differential.GigaToMega:
return UnitSize * POWER1;
case Differential.GigaToTerra:
return UnitSize / POWER1;
case Differential.TeraToByte:
return UnitSize * POWER4;
case Differential.TeraToKilo:
return UnitSize * POWER3;
case Differential.TeraToMega:
return UnitSize * POWER2;
case Differential.TeraToGiga:
return UnitSize * POWER1;
}
return 0;
}
}