Created
March 2, 2016 01:20
-
-
Save dwcullop/ac6d0f668b5e621f88f4 to your computer and use it in GitHub Desktop.
Helper Class for Displaying Quantities of Bytes in a user friendly way (e.g., "1.11 MB" instead of "1160165 Bytes") and an extension method class to facilitate calling it on various numeric datatypes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace DareWare.Utils | |
{ | |
/// <summary> | |
/// Static Helper Class that implements the ShowBytes functionality | |
/// </summary> | |
public static class ByteDisplay | |
{ | |
#region Private Fields | |
/// <summary>Suffixes to append to the formatted number value.</summary> | |
private static readonly string[] UNITS = { " Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB", " BB", " GpB" }; | |
#endregion Private Fields | |
#region Public Methods | |
/// <summary>Main worker function that formats the given value after scaling it appropriately</summary> | |
/// <param name="nByteCount">The number of bytes to be displayed</param> | |
/// <param name="format">Formatting string (e.g., "0.000" to display 3 decimal places)</param> | |
/// <returns>The Formatted String</returns> | |
static public string ShowBytes(decimal nByteCount, string format = null) | |
{ | |
if (String.IsNullOrEmpty(format)) format = "0.00"; | |
// Determine the power of 2 closest to the display value | |
double nPowerOfTwo = Math.Floor(Math.Log(Math.Abs(((double)nByteCount)), 2.0)); | |
if (nPowerOfTwo < 1.0) | |
{ | |
nPowerOfTwo = 0.0; | |
} | |
// Round power of two down to the nearest multiple of 10 | |
int nIndex = (int)Math.Floor(nPowerOfTwo / 10); | |
// Scale the display value by the rounded power of two | |
decimal nDisplayValue = decimal.Divide(nByteCount, (decimal)Math.Pow(2, 10 * nIndex)); | |
// Format it as a string with the units (If the index = 0, do not display a decimal so | |
// only whole numbers of bytes are shown) | |
return nDisplayValue.ToString((nIndex > 0) ? format : "0") + UNITS[nIndex]; | |
} | |
#endregion Public Methods | |
} | |
/// <summary>Extension class that facilitates displaying a quantity of Bytes</summary> | |
public static class ByteDisplayExtensions | |
{ | |
#region Public Methods | |
/// <summary>Display a long value as a scaled number of Bytes, using the optional formatting</summary> | |
/// <param name="value">Long Value to Display</param> | |
/// <param name="format">Optional Formatting</param> | |
/// <returns>String containing the Byte Information</returns> | |
/// <example>A value of '1,160,165' returns '1.11 MB'</example> | |
/// <example>A value of '94,462' returns '92.25 KB'</example> | |
/// <example>A value of '239,037' returns '233.43 KB'</example> | |
/// <example>A value of '94,462' returns '92.25 KB'</example> | |
/// <example>A value of '1,384,988' returns '1.32 MB'</example> | |
/// <example>A value of '1,023' returns '1023 Bytes'</example> | |
/// <example>A value of '2,048' returns '2.00 KB'</example> | |
/// <example>A value of '536,870,912' returns '512.00 MB'</example> | |
/// <example>A value of '1,073,741,824' returns '1024.00 MB'</example> | |
static public string ToByteDisplay(this long value, string format = null) | |
{ | |
return ByteDisplay.ShowBytes((decimal)value, format); | |
} | |
/// <summary>Display a ulong value as a scaled number of Bytes, using the optional formatting</summary> | |
/// <param name="value">Unsigned Long Value to Display</param> | |
/// <param name="format">Optional Formatting</param> | |
/// <returns>String containing the Byte Information</returns> | |
/// <example>A value of '1,160,165' returns '1.11 MB'</example> | |
/// <example>A value of '94,462' returns '92.25 KB'</example> | |
/// <example>A value of '239,037' returns '233.43 KB'</example> | |
/// <example>A value of '94,462' returns '92.25 KB'</example> | |
/// <example>A value of '1,384,988' returns '1.32 MB'</example> | |
/// <example>A value of '1,023' returns '1023 Bytes'</example> | |
/// <example>A value of '2,048' returns '2.00 KB'</example> | |
/// <example>A value of '536,870,912' returns '512.00 MB'</example> | |
/// <example>A value of '1,073,741,824' returns '1024.00 MB'</example> | |
static public string ToByteDisplay(this ulong value, string format = null) | |
{ | |
return ByteDisplay.ShowBytes((decimal)value, format); | |
} | |
/// <summary>Display an int value as a scaled number of Bytes, using the optional formatting</summary> | |
/// <param name="value">Int Value to Display</param> | |
/// <param name="format">Optional Formatting</param> | |
/// <returns>String containing the Byte Information</returns> | |
/// <example>A value of '1,160,165' returns '1.11 MB'</example> | |
/// <example>A value of '94,462' returns '92.25 KB'</example> | |
/// <example>A value of '239,037' returns '233.43 KB'</example> | |
/// <example>A value of '94,462' returns '92.25 KB'</example> | |
/// <example>A value of '1,384,988' returns '1.32 MB'</example> | |
/// <example>A value of '1,023' returns '1023 Bytes'</example> | |
/// <example>A value of '2,048' returns '2.00 KB'</example> | |
/// <example>A value of '536,870,912' returns '512.00 MB'</example> | |
/// <example>A value of '1,073,741,824' returns '1024.00 MB'</example> | |
static public string ToByteDisplay(this int value, string format = null) | |
{ | |
return ByteDisplay.ShowBytes((decimal)value, format); | |
} | |
/// <summary>Display a uint value as a scaled number of Bytes, using the optional formatting</summary> | |
/// <param name="value">Unsigned Int Value to Display</param> | |
/// <param name="format">Optional Formatting</param> | |
/// <returns>String containing the Byte Information</returns> | |
/// <example>A value of '1,160,165' returns '1.11 MB'</example> | |
/// <example>A value of '94,462' returns '92.25 KB'</example> | |
/// <example>A value of '239,037' returns '233.43 KB'</example> | |
/// <example>A value of '94,462' returns '92.25 KB'</example> | |
/// <example>A value of '1,384,988' returns '1.32 MB'</example> | |
/// <example>A value of '1,023' returns '1023 Bytes'</example> | |
/// <example>A value of '2,048' returns '2.00 KB'</example> | |
/// <example>A value of '536,870,912' returns '512.00 MB'</example> | |
/// <example>A value of '1,073,741,824' returns '1024.00 MB'</example> | |
static public string ToByteDisplay(this uint value, string format = null) | |
{ | |
return ByteDisplay.ShowBytes((decimal)value, format); | |
} | |
/// <summary>Display a float value as a scaled number of Bytes, using the optional formatting</summary> | |
/// <param name="value">Float Value to Display</param> | |
/// <param name="format">Optional Formatting</param> | |
/// <returns>String containing the Byte Information</returns> | |
/// <example>A value of '744.8683' returns '745 Bytes'</example> | |
/// <example>A value of '3209.751' returns '3.13 KB'</example> | |
/// <example>A value of '131072.0' returns '128.00 KB'</example> | |
/// <example>A value of '4194304.0' returns '4.00 MB'</example> | |
/// <example>A value of '536870900.0' returns '512.00 MB'</example> | |
/// <example>A value of '1073742000.0' returns '1.00 GB'</example> | |
static public string ToByteDisplay(this float value, string format = null) | |
{ | |
return ByteDisplay.ShowBytes((decimal)value, format); | |
} | |
/// <summary> | |
/// Display a double value as a scaled number of Bytes, using the optional formatting | |
/// </summary> | |
/// <param name="value">Double Value to Display</param> | |
/// <param name="format">Optional Formatting</param> | |
/// <returns>String containing the Byte Information</returns> | |
/// <example>A value of '634,982,839.306363' returns '605.57 MB'</example> | |
/// <example>A value of '1,888,566,572.36215' returns '1.76 GB'</example> | |
/// <example>A value of '549,755,813,888' returns '512.00 GB'</example> | |
/// <example>A value of '17,592,186,044,416' returns '16.00 TB'</example> | |
/// <example>A value of '2,251,799,813,685,250' returns '2.00 PB'</example> | |
static public string ToByteDisplay(this double value, string format = null) | |
{ | |
return ByteDisplay.ShowBytes((decimal)value, format); | |
} | |
/// <summary> | |
/// Display a decimal value as a scaled number of Bytes, using the optional formatting | |
/// </summary> | |
/// <param name="value">Decimal Value to Display</param> | |
/// <param name="format">Optional Formatting</param> | |
/// <returns>String containing the Byte Information</returns> | |
/// <example>A value of '101,763,413,460.055' returns '94.77 GB'</example> | |
/// <example>A value of '15,914,374,673,780.8' returns '14.47 TB'</example> | |
/// <example>A value of '524,288' returns '512.00 KB'</example> | |
/// <example>A value of '2,097,152' returns '2.00 MB'</example> | |
/// <example>A value of '1,073,741,824' returns '1.00 GB'</example> | |
/// <example>A value of '70,368,744,177,664' returns '64.00 TB'</example> | |
/// <example>A value of '9,007,199,254,740,990' returns '8.00 PB'</example> | |
/// <example>A value of '18,446,744,073,709,600,000' returns '16.00 EB'</example> | |
/// <example>A value of '151,115,727,451,829,000,000,000' returns '128.00 ZB'</example> | |
/// <example>A value of '618,970,019,642,690,000,000,000,000' returns '512.00 YB'</example> | |
/// <example>A value of '19,807,040,628,566,100,000,000,000,000' returns '16.00 BB'</example> | |
static public string ToByteDisplay(this decimal value, string format = null) | |
{ | |
return ByteDisplay.ShowBytes(value, format); | |
} | |
/// <summary> | |
/// Display a string value as a scaled number of Bytes, using the optional formatting | |
/// </summary> | |
/// <param name="value">String Value to Display</param> | |
/// <param name="format">Optional Formatting</param> | |
/// <returns>String containing the Byte Information</returns> | |
/// <example>A value of '101,763,413,460.055' returns '94.77 GB'</example> | |
/// <example>A value of '15,914,374,673,780.8' returns '14.47 TB'</example> | |
/// <example>A value of '524,288' returns '512.00 KB'</example> | |
/// <example>A value of '2,097,152' returns '2.00 MB'</example> | |
/// <example>A value of '1,073,741,824' returns '1.00 GB'</example> | |
/// <example>A value of '70,368,744,177,664' returns '64.00 TB'</example> | |
/// <example>A value of '9,007,199,254,740,990' returns '8.00 PB'</example> | |
/// <example>A value of '18,446,744,073,709,600,000' returns '16.00 EB'</example> | |
/// <example>A value of '151,115,727,451,829,000,000,000' returns '128.00 ZB'</example> | |
/// <example>A value of '618,970,019,642,690,000,000,000,000' returns '512.00 YB'</example> | |
/// <example>A value of '19,807,040,628,566,100,000,000,000,000' returns '16.00 BB'</example> | |
static public string ToByteDisplay(this string value, string format = null) | |
{ | |
return ByteDisplay.ShowBytes(Decimal.Parse(value, System.Globalization.NumberStyles.Any), format); | |
} | |
#endregion Public Methods | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How To Use It
Sample Values
Here are some sample values and the value that ShowBytes would return for each of them.