# DataUnitConvert Class

Nov 18th, 2018
293
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. using System;
2. using System.Collections.Generic;
3. using System.Linq;
4. using System.Text;
6.
7. /// <summary>
8. /// Class to convert/manage units of information.
9. /// https://en.wikipedia.org/wiki/Units_of_information
10. /// </summary>
11. public static class DataUnitConvert {
12.
13.     public static double Measurement = DataUnitMeasurement.Decimal;
14.
15.     /// <summary>
16.     /// Converts data from one unit of information to another to another.
17.     /// </summary>
18.     public static double UnitConvert(this double input, DataUnit originalType, DataUnit outputType) {
19.
20.         if (originalType == DataUnit.None || outputType == DataUnit.None)
21.             return double.NaN;
22.
23.         double difference = Math.Abs(outputType - originalType);
24.
25.         if (difference == 0)
26.             return input;
27.
28.         double multiplicative = Math.Pow(Measurement, difference);
29.
30.         if (difference > 0) // divide
31.             return input / multiplicative;
32.
33.         if (difference < 0) // multiply
34.             return input * multiplicative;
35.
36.         return double.NaN;
37.
38.     }
39.
40.     /// <summary>
41.     /// Gets the best unit of information to display data in.
42.     /// </summary>
43.     public static DataUnit GetDataUnit(this double input, DataUnit type = DataUnit.Byte) {
44.
45.         DataUnit unit = DataUnit.Byte;
46.
47.         while (true) {
48.
49.             double test = input.UnitConvert(type, unit);
50.
51.             if (test > Measurement)
52.                 unit++;
53.             else
54.                 break;
55.
56.         }
57.
58.         return unit;
59.
60.     }
61.
62.     /// <summary>
63.     /// Converts input to a string, appending the specified units abbreviaton.
64.     /// Will also round the nubmer for cleaner display.
65.     /// </summary>
66.     public static string ToString(this double input, DataUnit unit, int decimalPoints = 2) {
67.         string abbr = unit.GetAbbreviation().ToUpper();
68.         decimal dec = Convert.ToDecimal(input);
69.         dec = decimal.Round(dec, decimalPoints, MidpointRounding.AwayFromZero);
70.         if (unit == DataUnit.None)
71.             return Convert.ToString(dec);
72.         else
73.             return Convert.ToString(dec) + " " + abbr;
74.     }
75.
76.     /// <summary>
77.     /// Converts a string to it's original data.
78.     /// Specifying 'unit' will convert it to another unit of information automatically.
79.     /// </summary>
80.     public static double ToDouble(this string input, DataUnit unit = DataUnit.None) {
81.         string[] pieces = input.Split(new[] { ' ' });
82.         double number = Convert.ToDouble(pieces[0]);
83.         DataUnit originalUnit = GetDataUnit(pieces[1]);
84.         if (unit == DataUnit.None)
85.             return number;
86.         else
87.             return number.UnitConvert(originalUnit, unit);
88.     }
89.
90.     /// <summary>
91.     /// Returns an abbreviation of a specified DataUnit.
92.     /// </summary>
93.     public static string GetAbbreviation(this DataUnit unit) {
94.         switch (unit) {
95.             case DataUnit.Byte: return "b";
96.             case DataUnit.Kilobyte: return "kb";
97.             case DataUnit.Megabyte: return "mb";
98.             case DataUnit.Gigabyte: return "gb";
99.             case DataUnit.Terabyte: return "tb";
100.             case DataUnit.Petabyte: return "pb";
101.             case DataUnit.Exabyte: return "eb";
102.             case DataUnit.Zettabyte: return "zb";
103.             case DataUnit.Yottabyte: return "yb";
104.         }
105.         return string.Empty;
106.     }
107.
108.     /// <summary>
109.     /// Returns a DataUnit based on the specified abbreviation.
110.     /// </summary>
111.     public static DataUnit GetDataUnit(this string unit) {
112.         switch (unit.ToLower()) {
113.             case "b": return DataUnit.Byte;
114.             case "kb": return DataUnit.Kilobyte;
115.             case "mb": return DataUnit.Megabyte;
116.             case "gb": return DataUnit.Gigabyte;
117.             case "tb": return DataUnit.Terabyte;
118.             case "pb": return DataUnit.Petabyte;
119.             case "eb": return DataUnit.Exabyte;
120.             case "zb": return DataUnit.Zettabyte;
121.             case "yb": return DataUnit.Yottabyte;
122.         }
123.         return DataUnit.None;
124.     }
125.
126.     // === CONVERTED FUNCTION ===
127.
128.     /// <summary>
129.     /// Converts data from one unit of information to another to another.
130.     /// </summary>
131.     public static double UnitConvert(this long input, DataUnit originalType, DataUnit outputType) {
132.         double converted = Convert.ToDouble(input);
133.         return converted.UnitConvert(originalType, outputType);
134.     }
135.
136.     /// <summary>
137.     /// Gets the best unit of information to display data in.
138.     /// </summary>
139.     public static DataUnit GetDataUnit(this long bytes) {
140.         double converted = Convert.ToDouble(bytes);
141.         return converted.GetDataUnit();
142.     }
143.
144. }
145.
146. /// <summary>
147. /// List of supported units of information.
148. /// In order, least to greatest.
149. /// </summary>
150. public enum DataUnit {
151.     None,
152.     Byte,
153.     Kilobyte,
154.     Megabyte,
155.     Gigabyte,
156.     Terabyte,
157.     Petabyte,
158.     Exabyte,
159.     Zettabyte,
160.     Yottabyte
161. }
162.
163. /// <summary>
164. /// Measurements to convert data.
165. /// Note: Decimal is more common, and should be used for displaying data.
166. /// </summary>
167. public struct DataUnitMeasurement {
168.     public const double Binary = 1024.0;
169.     public const double Decimal = 1000.0;
170. }
RAW Paste Data