Converting file length into User friendly format (GB, MB, KB, Bytes)

Use following function to convert file length in bytes to user friendly format


private string FormatByteSize(long length)
{
const int KB = 1024;
const int MB = 1024 * KB;
const int GB = 1024 * MB;
if (length > GB)
{
return (length / GB).ToString("0.00") + " GB";
}
else if (length > MB)
{
return (length / MB).ToString("0.00") + " MB";
}
else if (length > KB)
{
return (length / KB).ToString("0.00") + " KB";
}
return length.ToString()+" Bytes";
}

1 comments:

E said...

Thanks! I just need to improve it by adding rounding.