Ejemplo n.º 1
0
        public static FileSizeFormatSize GetFileSizeRange(decimal size)
        {
            FileSizeFormatSize range  = FileSizeFormatSize.PB;
            string             suffix = null;

            while (suffix == null)
            {
                if (size > sizes[(int)range])
                {
                    size  /= sizes[(int)range];
                    suffix = range.ToString();
                }
                else
                {
                    if (--range == 0)
                    {
                        suffix = range.ToString();
                    }
                }
            }
            return(range);
        }
Ejemplo n.º 2
0
        public string Format(string format, object arg, IFormatProvider formatProvider)
        {
            if (format == null || !format.StartsWith(fileSizeFormat))
            {
                return(defaultFormat(format, arg, formatProvider));
            }

            if (arg is string)
            {
                return(defaultFormat(format, arg, formatProvider));
            }
            string             precision;
            FileSizeFormatSize range = FileSizeFormatSize.B;
            bool hasRange            = false;
            bool noSuffix            = false;

            if (format.Contains(","))
            {
                var args = format.Split(',');
                precision = args[0].Substring(2);
                string r = args[1];
                if (r.EndsWith("!"))
                {
                    noSuffix = true;
                    r        = r.Substring(0, r.Length - 1);
                }
                hasRange = true;
                range    = (FileSizeFormatSize)Enum.Parse(typeof(FileSizeFormatSize), r);
            }
            else
            {
                range     = FileSizeFormatSize.PB;
                precision = format.Substring(2);
            }

            if (String.IsNullOrEmpty(precision))
            {
                precision = _default_precision.ToString();
            }

            Decimal size;

            try
            {
                size = Convert.ToDecimal(arg);
            }
            catch (InvalidCastException)
            {
                return(defaultFormat(format, arg, formatProvider));
            }

            string suffix = null;


            if (hasRange)
            {
                suffix = range.ToString();
                size  /= sizes[(int)range];
            }
            else
            {
                while (suffix == null)
                {
                    if (size > sizes[(int)range])
                    {
                        size  /= sizes[(int)range];
                        suffix = range.ToString();
                    }
                    else
                    {
                        if (--range == 0)
                        {
                            suffix = range.ToString();
                        }
                    }
                }
            }
            if (range == FileSizeFormatSize.B || size == 0)
            {
                precision = "0";
            }

            return(noSuffix ? String.Format("{0:N" + precision + "}", size) : String.Format("{0:N" + precision + "} {1}", size, suffix));
        }
Ejemplo n.º 3
0
 public static string ToFileSizeString(this long size, FileSizeFormatSize range, bool suffix = true, int precision = default_precision)
 {
     return(String.Format(_talk_bytes, "{0:fs" + precision.ToString() + "," + range.ToString() + (suffix ? "":"!") + "}", size));
 }