Exemple #1
0
        /// <summary>
        /// Returns a human readable string representing the specified Int64 size using a specified size suffix format method.
        /// </summary>
        /// <param name="size">a Int64 defining the size</param>
        /// <param name="standard">the method/suffixes to use</param>
        /// <returns>A human readable string of a given size.</returns>
        public static string ToHumanReadableSize(this long size, SizeFormatStandards standard)
        {
            var items = GetSizeFormatItems(standard);

            if (items == null)
            {
                throw new ArgumentOutOfRangeException("standard", "The specified standard is not supported");
            }

            var q = items.OrderByDescending(s => s.Power);

            double pce    = 0;
            string factor = "B";

            foreach (SizeFormatItem s in q)
            {
                pce = Math.Pow(s.Base, s.Power);
                if (size >= pce)
                {
                    factor = s.Factor;
                    break;
                }
            }

            var value = pce > 0 ? size / pce : size;

            return(string.Format(
                       "{0:0.00} {1}",
                       value,
                       factor));
        }
Exemple #2
0
        /// <summary>
        /// Returns a human readable string representing the specified Int64 size using a specified size suffix format method.
        /// </summary>
        /// <param name="size">a Int64 defining the size</param>
        /// <param name="standard">the method/suffixes to use</param>
        /// <returns>A human readable string of a given size.</returns>
        public static string ToHumanReadableSize(this long size, SizeFormatStandards standard)
        {
            var items = GetSizeFormatItems(standard);

            if (items == null)
            {
                throw new ArgumentOutOfRangeException("standard", "The specified standard is not supported");
            }

            var q = items.OrderByDescending(s => s.Power);

            double pce = 0;
            string factor = "B";

            foreach (SizeFormatItem s in q)
            {
                pce = Math.Pow(s.Base, s.Power);
                if (size >= pce)
                {
                    factor = s.Factor;
                    break;
                }
            }

            var value = pce > 0 ? size / pce : size;

            return string.Format(
                "{0:0.00} {1}",
                value,
                factor);
        }
Exemple #3
0
        /// <summary>
        /// Gets the size format array for the specified standard.
        /// </summary>
        /// <param name="standard">The size formatting standard.</param>
        /// <returns>An array of <see cref="SizeFormatItem"/>, representing the specified standard.</returns>
        private static SizeFormatItem[] GetSizeFormatItems(SizeFormatStandards standard)
        {
            switch (standard)
            {
            case SizeFormatStandards.SI: return(si);

            case SizeFormatStandards.Binary: return(bi);

            default:
                return(null);
            }
        }
Exemple #4
0
 /// <summary>
 /// Gets the size format array for the specified standard.
 /// </summary>
 /// <param name="standard">The size formatting standard.</param>
 /// <returns>An array of <see cref="SizeFormatItem"/>, representing the specified standard.</returns>
 private static SizeFormatItem[] GetSizeFormatItems(SizeFormatStandards standard)
 {
     switch (standard)
     {
         case SizeFormatStandards.SI: return si;
         case SizeFormatStandards.Binary: return bi;
         default:
             return null;
     }
 }