Just for sharing, I found this small code from this site and this is the easiest way for converting bytes (because I’m a Math guru who repeated College Algebra by 5 times that is why I picked this code rather dividing large numbers to 1024 by 1024 and so on…).So let Microsoft do the Math Okay! .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | private string ToFileSize(long source) { const int byteConversion = 1024; double bytes = Convert.ToDouble(source); if (bytes >= Math.Pow(byteConversion, 4)) //TB Range { return string.Concat(Math.Round(bytes / Math.Pow(byteConversion, 4), 2), " TB"); } if (bytes >= Math.Pow(byteConversion, 3)) //GB Range { return string.Concat(Math.Round(bytes / Math.Pow(byteConversion, 3), 2), " GB"); } else if (bytes >= Math.Pow(byteConversion, 2)) //MB Range { return string.Concat(Math.Round(bytes / Math.Pow(byteConversion, 2), 2), " MB"); } else if (bytes >= byteConversion) //KB Range { return string.Concat(Math.Round(bytes / byteConversion, 2), " KB"); } else //Bytes { return string.Concat(bytes, " Bytes"); } } |
Another useful diagram I found (sorry but i lost the source of this picture, an apology to the author)


