Feedback

C# - Kapazität mit Einheit darstellen

Veröffentlicht von am 12/28/2007
(1 Bewertungen)
Dieses Snippet kann eine in Bytes gemessene Kapazität darstellen.
Von Byte, über MegaByte bis YottaByte sind möglich.
Desweiteren ist eine Umwandlung eines Strings in bytes möglich.
using System;

namespace ViperBytes.IO
{
    /// <summary>
    /// Handles a capacity in bytes
    /// </summary>
    class Capacity
    {
        #region statics
        /// <summary>
        /// The units of byte
        /// </summary>
        public static string[] Units = new string[] { "Byte", "KiloByte", "MegaByte", "GigaByte", "TeraByte", "PetaByte", "ExaByte", "ZettaByte", "YottaByte" };

        /// <summary>
        /// Short forms of the units
        /// </summary>
        public static string[] ShortUnits = new string[] { "Byte", "KByte", "MByte", "GByte", "TByte", "PByte", "EByte", "ZByte", "YByte" };

        /// <summary>
        /// Shortest forms of units
        /// </summary>
        public static string[] VeryShortUnits = new string[] { "Byte", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
        #endregion

        #region fields
        /// <summary>
        /// Contains the number of bytes
        /// </summary>
        private decimal bytes;
        #endregion

        #region properties
        /// <summary>
        /// Contains the bytes, read only
        /// </summary>
        public decimal Bytes
        {
            get
            {
                return this.bytes;
            }
        }
        #endregion

        #region constructor
        /// <summary>
        /// Constructor
        /// </summary>
        public Capacity()
        {
            this.bytes = 0;
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="bytes">Number of bytes</param>
        public Capacity(decimal bytes)
        {
            this.bytes = bytes;
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="size">Size string in form of a number, followed of a space and the unit</param>
        public Capacity(string size)
        {
            decimal number;
            int unit;

            // the first is, to get the separator, that is a space char
            if (size.Contains(" "))
            {
                int i = size.LastIndexOf(" ");

                try
                {
                    number = Decimal.Parse(size.Substring(0, i));
                    unit = GetStringUnit(size.Substring(i + 1));
                }
                catch
                {
                    number = 0;
                    unit = 0;
                }
            }
            else
            {
                // no separator found - no unit!
                try
                {
                    number = Decimal.Parse(size);
                    unit = 0;
                }
                catch
                {
                    number = 0;
                    unit = 0;
                }
            }

            // set bytes
            try
            {
                this.bytes = Math.Floor(number * (decimal)Math.Pow(1024, unit));
            }
            catch
            {
                this.bytes = 0;
            }
        }
        #endregion

        #region public members
        /// <summary>
        /// Returns size in string without unit
        /// </summary>
        /// <returns>Size in string</returns>
        public override string ToString()
        {
            return ToString(GetOptimalUnit());
        }

        /// <summary>
        /// Returns size in string without unit
        /// </summary>
        /// <param name="unit">The unit string</param>
        /// <returns>Size in string</returns>
        public string ToString(string unit)
        {
            return ToString(GetStringUnit(unit));
        }

        /// <summary>
        /// Returns size in string without unit
        /// </summary>
        /// <param name="unit">The unit integer from 0 to 9</param>
        /// <returns>Size in string</returns>
        public string ToString(int unit)
        {
            return (Bytes / (decimal)Math.Pow(1024, unit)).ToString("n");
        }

        /// <summary>
        /// Returns size in string with unit
        /// </summary>
        /// <returns>Size in string</returns>
        public string ToUnitString()
        {
            return ToUnitString(GetOptimalUnit());
        }

        /// <summary>
        /// Returns size in string with unit
        /// </summary>
        /// <param name="unit">The unit string</param>
        /// <returns>Size in string</returns>
        public string ToUnitString(string unit)
        {
            return ToUnitString(GetStringUnit(unit));
        }

        /// <summary>
        /// Returns size in string with unit
        /// </summary>
        /// <param name="unit">The unit integer from 0 to 9</param>
        /// <returns>Size in string</returns>
        public string ToUnitString(int unit)
        {
            return (Bytes / (decimal)Math.Pow(1024, unit)).ToString("n") + " " + Units[unit];
        }

        /// <summary>
        /// Returns size in string with short unit
        /// </summary>
        /// <returns>Size in string</returns>
        public string ToShortUnitString()
        {
            return ToShortUnitString(GetOptimalUnit());
        }

        /// <summary>
        /// Returns size in string with short unit
        /// </summary>
        /// <param name="unit">The unit string</param>
        /// <returns>Size in string</returns>
        public string ToShortUnitString(string unit)
        {
            return ToShortUnitString(GetStringUnit(unit));
        }

        /// <summary>
        /// Returns size in string with short unit
        /// </summary>
        /// <param name="unit">The unit integer from 0 to 9</param>
        /// <returns>Size in string</returns>
        public string ToShortUnitString(int unit)
        {
            return (Bytes / (decimal)Math.Pow(1024, unit)).ToString("n") + " " + ShortUnits[unit];
        }

        /// <summary>
        /// Returns size in string with very short unit
        /// </summary>
        /// <returns>Size in string</returns>
        public string ToVeryShortUnitString()
        {
            return ToVeryShortUnitString(GetOptimalUnit());
        }

        /// <summary>
        /// Returns size in string with very short unit
        /// </summary>
        /// <param name="unit">The unit string</param>
        /// <returns>Size in string</returns>
        public string ToVeryShortUnitString(string unit)
        {
            return ToVeryShortUnitString(GetStringUnit(unit));
        }

        /// <summary>
        /// Returns size in string with very short unit
        /// </summary>
        /// <param name="unit">The unit integer from 0 to 9</param>
        /// <returns>Size in string</returns>
        public string ToVeryShortUnitString(int unit)
        {
            return (Bytes / (decimal)Math.Pow(1024, unit)).ToString("n") + " " + VeryShortUnits[unit];
        }
        #endregion

        #region privates members
        /// <summary>
        /// Gets optimal display unit
        /// </summary>
        /// <returns>The unit from 0 (byte) to 8 (yottabyte)</returns>
        private int GetOptimalUnit()
        {
            int unit = 0;
            // get optimal unit
            while (true)
            {
                if (Bytes < (decimal)Math.Pow(1024, unit))
                {
                    if (unit > 0)
                        unit--;
                    break;
                }

                if (unit < Units.Length - 1)
                    unit++;
                else
                    break;
            }

            return unit;
        }

        /// <summary>
        /// Gets a unit number from a string containing the unit
        /// </summary>
        /// <param name="unit">Contains the unit</param>
        /// <returns>An integer from 0 to 8</returns>
        private int GetStringUnit(string unit)
        {
            for (int i = 0; i < 9; i++)
            {
                if ((Units[i] == unit) || (ShortUnits[i] == unit) || (VeryShortUnits[i] == unit))
                    return i;
            }
            return 0;
        }
        #endregion
    }
}

Kommentare zum Snippet

 

Logge dich ein, um hier zu kommentieren!