To Display Byte size - CSharp System

CSharp examples for System:Byte

Description

To Display Byte size

Demo Code


using System.Windows;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;//from   w w  w.j  a v a2  s.c om

public class Main{
        public static string ToDisplayByte(long bytes)
        {
            string[] levelSubfix = new string[] { "Byte(s)", "KiB", "MiB", "GiB" };
            long[] levelValue = new long[] { 1, 1024, 1024 * 1024, 1024 * 1024 * 1024 };
            int level = 1;
            bool done = false;

            while (!done && level < levelValue.Length)
            {
                if (((double)bytes / (double)levelValue[level]) > 1)
                    level++;
                else
                {
                    done = true;
                    level--;
                }
            }
            if (level >= levelValue.Length) level = 3;
            return ((double)bytes / (double)levelValue[level]).ToString("F2") + " " + levelSubfix[level];
        }
}

Related Tutorials