Converting Bytes to Human Readable Value#

/// <summary>
/// Round up to the best human readable number
/// (could be kb, mb or gb depending on size of
/// number
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public string BytesToHumanReadable(double bytes)
{
var gig = Math.Pow(1024, 3);
var meg = Math.Pow(1024, 2);

if(bytes > gig)
return Math.Round(bytes / gig, 1) + " GB";
else if (bytes > meg)
return (int)(bytes / meg ) + " MB";
else
return (int)(bytes / 1024 ) + " KB";

}


This is useful for file and memory sizes. 

Monday, October 06, 2008 10:42:20 AM (Central Standard Time, UTC-06:00) #    Comments [0]  | 

 

All content © 2009, prag
On this page
This site
Calendar
<January 2009>
SunMonTueWedThuFriSat
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567
Archives
Sitemap
Blogroll OPML
Disclaimer

Powered by: newtelligence dasBlog 1.9.6264.0

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Send mail to the author(s) E-mail

Theme design by Jelle Druyts