Skip to content

Instantly share code, notes, and snippets.

@dwcullop
Created March 2, 2016 01:20
Show Gist options
  • Save dwcullop/ac6d0f668b5e621f88f4 to your computer and use it in GitHub Desktop.
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
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
}
}
@dwcullop
Copy link
Author

dwcullop commented Mar 2, 2016

How To Use It

// Basic Invocation
decimal someByteCount = 9392334924982489242;
Console.WriteLine( ByteDisplay.ShowBytes( someByteCount ) );

// Via the extension methods
long byteCount = 12345668928;
Console.WriteLine( byteCount.ToByteDisplay() );

Sample Values

Here are some sample values and the value that ShowBytes would return for each of them.

0.000 0 Bytes
63253.642 61.77 KB
12367.397 12.08 KB
37443.303 36.57 KB
36260.391 35.41 KB
91219.321 89.08 KB
253791.243 247.84 KB
182293.152 178.02 KB
369378.153 360.72 KB
351102.342 342.87 KB
409366.637 399.77 KB
345117.254 337.03 KB
345039.634 336.95 KB
90316.244 88.20 KB
317862.844 310.41 KB
427919.711 417.89 KB
79070.815 77.22 KB
771552.013 753.47 KB
852190.860 832.22 KB
454993.707 444.33 KB
249411.506 243.57 KB
715148.627 698.39 KB
845638.112 825.82 KB
1237887.396 1.18 MB
105678.081 103.20 KB
260215.111 254.12 KB
760129.119 742.31 KB
1748221.525 1.67 MB
252283.891 246.37 KB
201818.453 197.09 KB
1255460.005 1.20 MB
506881.434 495.00 KB
1538668.753 1.47 MB
2016011.450 1.92 MB
1268313.784 1.21 MB
1047905.392 1023.35 KB
1118660.367 1.07 MB
2409275.471 2.30 MB
2081856.203 1.99 MB
1153625.625 1.10 MB
2519466.735 2.40 MB
257200.247 251.17 KB
2736675.121 2.61 MB
2602586.734 2.48 MB
2681275.933 2.56 MB
1429230.962 1.36 MB
2476818.622 2.36 MB
878837.533 858.24 KB
2027275.576 1.93 MB
2501172.088 2.39 MB
1899147.105 1.81 MB
3211357.781 3.06 MB
212436.656 207.46 KB
115082.898 112.39 KB
2947133.877 2.81 MB
826866.012 807.49 KB
1389670.593 1.33 MB
1020214.932 996.30 KB
468515.170 457.53 KB
3787357.111 3.61 MB
3272647.734 3.12 MB
2628221.298 2.51 MB
1055967.872 1.01 MB
2470419.763 2.36 MB
4012717.086 3.83 MB
1598067.231 1.52 MB
3222575.220 3.07 MB
2224269.133 2.12 MB
2392243.861 2.28 MB
4022723.099 3.84 MB
4409305.714 4.21 MB
380728.458 371.81 KB
4041772.057 3.85 MB
1296756.847 1.24 MB
1960731.295 1.87 MB
464804.892 453.91 KB
2616814.145 2.50 MB
3345559.379 3.19 MB
4556689.936 4.35 MB
3016106.663 2.88 MB
1768057.708 1.69 MB
853502.219 833.50 KB
1846151.079 1.76 MB
1950650.284 1.86 MB
2899034.451 2.76 MB
3775433.713 3.60 MB
1054849.656 1.01 MB
3994346.009 3.81 MB
573293.846 559.86 KB
1981978.298 1.89 MB
461214.698 450.40 KB
1198960.628 1.14 MB
5247468.909 5.00 MB
5220015.240 4.98 MB
3813769.442 3.64 MB
1890601.965 1.80 MB
5258633.941 5.02 MB
2516754.177 2.40 MB
6209531.705 5.92 MB
3598891.529 3.43 MB
4846979.076 4.62 MB
2085229.468 1.99 MB
5003570.641 4.77 MB
5095469.024 4.86 MB
5573385.657 5.32 MB
3425159.791 3.27 MB
724200.120 707.23 KB
5548755.484 5.29 MB
4450788.711 4.24 MB
5273327.864 5.03 MB
5075197.868 4.84 MB
5206202.275 4.97 MB
2801725.453 2.67 MB
4004186.911 3.82 MB
4798462.014 4.58 MB
5060812.414 4.83 MB
2386300.671 2.28 MB
6910713.720 6.59 MB
554682.270 541.68 KB
2175538.634 2.07 MB
1478394.724 1.41 MB
352699.775 344.43 KB
3255367.965 3.10 MB
1588008.437 1.51 MB
6593790.150 6.29 MB
6226836.265 5.94 MB
1746444.057 1.67 MB
828.915 829 Bytes
5469155.467 5.22 MB
850793.124 830.85 KB
2204275.018 2.10 MB
1111645.788 1.06 MB
6022137.619 5.74 MB
2934866.054 2.80 MB
7402799.795 7.06 MB
196409.481 191.81 KB
3674509.241 3.50 MB
5285386.636 5.04 MB
4891763.825 4.67 MB
262952.618 256.79 KB
5261870.685 5.02 MB
3658127.375 3.49 MB
667544.345 651.90 KB
3385031.375 3.23 MB
6000084.956 5.72 MB
3444893.262 3.29 MB
7839613.910 7.48 MB
3175556.201 3.03 MB
2077538.764 1.98 MB
9253041.679 8.82 MB
9729827.371 9.28 MB
9248373.656 8.82 MB
3229273.766 3.08 MB
6897351.868 6.58 MB
3701002.483 3.53 MB
7061012.071 6.73 MB
9281764.805 8.85 MB
5674604.299 5.41 MB
9769273.674 9.32 MB
600313.496 586.24 KB
3001584.372 2.86 MB
1098913.504 1.05 MB
1479019.629 1.41 MB
5642360.900 5.38 MB
5153403.658 4.91 MB
6212632.260 5.92 MB
212557.772 207.58 KB
492440.211 480.90 KB
7682053.311 7.33 MB
6867633.905 6.55 MB
2888064.410 2.75 MB
8127303.905 7.75 MB
4062358.827 3.87 MB
485948.653 474.56 KB
8044550.118 7.67 MB
9084563.293 8.66 MB
11219974.949 10.70 MB
533087.874 520.59 KB
6546729.829 6.24 MB
5265793.799 5.02 MB
11097846.798 10.58 MB
10460758.161 9.98 MB
9373928.711 8.94 MB
8447797.199 8.06 MB
1336903.470 1.27 MB
3930223.994 3.75 MB
9816451.982 9.36 MB
101264.900 98.89 KB
11951193.540 11.40 MB
1831383.163 1.75 MB
1423554.614 1.36 MB
10774400.510 10.28 MB
8118646.744 7.74 MB
6112697.422 5.83 MB
9441561.568 9.00 MB
5998051.973 5.72 MB
3295625.337 3.14 MB
7017397.177 6.69 MB
11441302.298 10.91 MB
798973.043 780.25 KB
4495483.211 4.29 MB
10200195.623 9.73 MB
8365206.897 7.98 MB
7904117.024 7.54 MB
9202916.685 8.78 MB
3554171.674 3.39 MB
7751603.331 7.39 MB
3816307.972 3.64 MB
13391806.378 12.77 MB
7870248.922 7.51 MB
9941622.373 9.48 MB
11918692.948 11.37 MB
13758926.218 13.12 MB
6903969.764 6.58 MB
1638566.359 1.56 MB
5697856.543 5.43 MB
4504992.532 4.30 MB
6185134.633 5.90 MB
5962285.027 5.69 MB
2229143.845 2.13 MB
11090572.497 10.58 MB
163306.840 159.48 KB
1091033.512 1.04 MB
8036172.905 7.66 MB
7424366.725 7.08 MB
5875681.568 5.60 MB
1185025.248 1.13 MB
13048495.245 12.44 MB
4486380.766 4.28 MB
3543501.520 3.38 MB
8072396.310 7.70 MB
6497695.558 6.20 MB
2497118.280 2.38 MB
7634114.787 7.28 MB
1624059.776 1.55 MB
2563253.694 2.44 MB
3866229.562 3.69 MB
2978750.081 2.84 MB
250016.509 244.16 KB
13255981.306 12.64 MB
11796314.793 11.25 MB
8276025.113 7.89 MB
409909.043 400.30 KB
6296976.854 6.01 MB
6803890.977 6.49 MB
4052715.585 3.86 MB
14033286.112 13.38 MB
2438288.874 2.33 MB
5955758.495 5.68 MB
5519380.014 5.26 MB
2475698.495 2.36 MB
13515720.093 12.89 MB
2084386.286 1.99 MB
12044220.853 11.49 MB
4861368.673 4.64 MB
5543310.125 5.29 MB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment