I've got this function that converts bytes to a more readable format (ie. KB, MB, GB, etc):
It works great with one exception, it rounds everything. For instance, if I have this stored in my DB '3556352' and once put through the above function, it spits out 3MB, when in reality, it's 3,473KB or 3.4MB. Is there anyway to get it to display 3.4MB instead of 3?
Thanks!
Code:
public string ConvertBytes(long lBytes)
{
string sSize = string.Empty;
if (lBytes >= 1073741824)
sSize = String.Format("{0:##.##}", lBytes / 1073741824) + " GB";
else if (lBytes >= 1048576)
sSize = String.Format("{0:D}", lBytes / 1048576) + " MB";
else if (lBytes >= 1024)
sSize = String.Format("{0:##.##}", lBytes / 1024) + " KB";
else if (lBytes > 0 && lBytes < 1024)
sSize = lBytes.ToString() + " bytes";
return sSize;
}
It works great with one exception, it rounds everything. For instance, if I have this stored in my DB '3556352' and once put through the above function, it spits out 3MB, when in reality, it's 3,473KB or 3.4MB. Is there anyway to get it to display 3.4MB instead of 3?
Thanks!