﻿using System;

namespace GUtils.IO
{
    public static class FileSizes
    {
        public const UInt64 B = 1024;
        public const UInt64 KiB = 1024 * B;
        public const UInt64 MiB = 1024 * KiB;
        public const UInt64 GiB = 1024 * MiB;
        public const UInt64 TiB = 1024 * GiB;
        public const UInt64 PiB = 1024 * TiB;

        private static readonly String[] _suffixes = new[] { "B", "KiB", "MiB", "GiB", "TiB", "PiB" };

        public static String Format ( UInt64 Size )
        {
            var i = ( Int32 ) Math.Floor ( Math.Log ( Size, 1024 ) );
            return $"{Size / ( Math.Pow ( B, i ) )} {_suffixes[i]}";
        }
    }
}

﻿using System;

namespace GUtils.IO
{
	public static class FileSizes
	{
		public const UInt64 B = 1024;
		public const UInt64 KiB = 1024 * B;
		public const UInt64 MiB = 1024 * KiB;
		public const UInt64 GiB = 1024 * MiB;
		public const UInt64 TiB = 1024 * GiB;
		public const UInt64 PiB = 1024 * TiB;

		private static readonly String[] _suffixes = new[] { "B", "KiB", "MiB", "GiB", "TiB", "PiB" };

		public static String Format ( UInt64 Size )
		{
			var i = ( Int32 ) Math.Floor ( Math.Log ( Size, 1024 ) );
			return $"{Size / ( Math.Pow ( B, i ) )} {_suffixes[i]}";
		}

		public static String Format ( UInt64 Size, Int32 decimals )
		{
			var i = ( Int32 ) Math.Floor ( Math.Log ( Size, 1024 ) );
			return $"{Math.Round ( Size / ( Math.Pow ( B, i ) ), decimals )} {_suffixes[i]}";
		}
	}
}

Add an option to set the number of decimal places that will be used in the formatting
Add an option to set the number of decimal places that will be used in the formatting
