/ Published in: C#
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
namespace Kyrathasoft.Conversions.GetFilesizeDescription { using System; public static class clsGetFilesizeDescription { public static string getFilesizeDesc(double sizeInBytes) { const double Terabyte = 1099511627776; const double Gigabyte = 1073741824; const double Megabyte = 1048576; const double Kilobyte = 1024; string result = string.Empty; double the_size = 0; string units = string.Empty; if (sizeInBytes >= Terabyte) { the_size = sizeInBytes / Terabyte; units = " Tb"; } else { if (sizeInBytes >= Gigabyte) { the_size = sizeInBytes / Gigabyte; units = " Gb"; } else { if (sizeInBytes >= Megabyte) { the_size = sizeInBytes / Megabyte; units = " Mb"; } else { if (sizeInBytes >= Kilobyte) { the_size = sizeInBytes / Kilobyte; units = " Kb"; } else { the_size = sizeInBytes; units = " bytes"; } } } } if (units != "bytes") { result = the_size.ToString("N3") + " " + units; } else { result = the_size.ToString() + " " + units; } return result; } } }