Exemplo n.º 1
0
        //  <summary>
        //  Returns a formatted size in bytes or its multiples.
        //  </summary>
        //  <date>Apr 15, 2009</date>
        private static string FormatSize(long bytes, string numericFormat = null, BytesSizeFormat bytesFormat = BytesSizeFormat.JEDEC)
        {
            if ((bytes < 0))
            {
                return("-" + FormatSize((bytes * -1), numericFormat, bytesFormat));
            }

            if (numericFormat == null)
            {
                numericFormat = "0.0";
            }
            if ((bytes == 1))
            {
                return("1 byte");
            }
            var mult = bytesFormat == BytesSizeFormat.SI ? 1000 : 1024;

            if ((bytes < mult))
            {
                return(bytes.ToString() + " bytes");
            }
            long limit = 1;
            int  unit  = -1;

            while (limit * mult <= bytes)
            {
                limit *= mult;
                unit++;
            }

            return(Math.Round((double)bytes / limit, 2).ToString(numericFormat) +
                   " " +
                   (unit == 0 && bytesFormat == BytesSizeFormat.SI ? "k" : Units[unit]) +
                   (bytesFormat == BytesSizeFormat.IEC ? "i" : "B"));
        }
Exemplo n.º 2
0
        public string ToString(string numericFormat = null, BytesSizeFormat bytesFormat = BytesSizeFormat.JEDEC)
        {
            return FormatSize(bytes, numericFormat, bytesFormat);

        }
Exemplo n.º 3
0
        //  <summary>
        //  Returns a formatted size in bytes or its multiples.
        //  </summary>
        //  <date>Apr 15, 2009</date>
        private static string FormatSize(long bytes, string numericFormat = null, BytesSizeFormat bytesFormat = BytesSizeFormat.JEDEC)
        {
            if ((bytes < 0))
            {
                return ("-" + FormatSize((bytes * -1), numericFormat, bytesFormat));
            }

            if (numericFormat == null) numericFormat = "0.0";
            if ((bytes == 1))
            {
                return "1 byte";
            }
            var mult = bytesFormat == BytesSizeFormat.SI ? 1000 : 1024;

            if ((bytes < mult))
            {
                return bytes.ToString() + " bytes";
            }
            long limit = 1;
            int unit = -1;
            while (limit * mult <= bytes)
            {
                limit *= mult;
                unit++;
            }

            return Math.Round((double)bytes / limit, 2).ToString(numericFormat) +
                " " +
                (unit == 0 && bytesFormat == BytesSizeFormat.SI ? "k" : Units[unit]) +
               (bytesFormat == BytesSizeFormat.IEC ? "i" : "B");

        }
Exemplo n.º 4
0
 public string ToString(string numericFormat = null, BytesSizeFormat bytesFormat = BytesSizeFormat.JEDEC)
 {
     return(FormatSize(bytes, numericFormat, bytesFormat));
 }