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]  | 

 

Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

All content © 2009, prag