Format Size as readable string - CSharp System.IO

CSharp examples for System.IO:File Size

Description

Format Size as readable string

Demo Code


using System.Linq;
using System;/* w  w  w. j av a2  s .co m*/

public class Main{
        public static string FormatSize(double bytes, string[] orders = null)
        {
            const int scale = 1024;

            if (orders == null)
                orders = new[] { "GB", "MB", "KB", "Bytes" };

            long max = (long)Math.Pow(scale, orders.Length - 1);

            foreach (string order in orders)
            {
                if (bytes > max)
                    return string.Format("{0:##.##} {1}", decimal.Divide((decimal)bytes, max), order);

                max /= scale;
            }
            return "0 Bytes";
        }
}

Related Tutorials