Exemple #1
0
        public static string ToBinaryUnit(this Int64 value, BinaryUnit unit, int decimalPlaces = 0)
        {
            string format = "N" + decimalPlaces.ToString();
            int    mult   = getMultiplier(unit);
            string rc     = (value / (double)Math.Pow(mult, Math.Abs((Int64)unit))).ToString(format);

            rc += unit.ToString();
            return(rc);
        }
Exemple #2
0
        private static int getMultiplier(BinaryUnit unit)
        {
            // 1K can be 1024 (IEC/JEDEC) bytes or 1000 bytes (metric)
            // See: https://en.wikipedia.org/wiki/Kilobyte
            int rc = 0;

            if (unit == BinaryUnit.KiB ||
                unit == BinaryUnit.MiB ||
                unit == BinaryUnit.GiB ||
                unit == BinaryUnit.TiB ||
                unit == BinaryUnit.PiB ||
                unit == BinaryUnit.EiB ||
                unit == BinaryUnit.ZiB ||
                unit == BinaryUnit.YiB)
            {
                rc = 1024;
            }
            else
            {
                rc = 1000;
            }
            return(rc);
        }