/// <summary> Bytes to KB, MB, GB, TB units intelligent conversion /// </summary> /// <param name="len"></param> /// <returns></returns> public static string ConvertBytes(long len) { //string[] sizes = { "Bytes", "KB", "MB", "GB", "TB" }; //int order = 0; //while (len >= 1024 && order + 1 < sizes. Length) //{ // order++; // len = len / 1024; //} //return String.Format("{0:0.##} {1}", len, sizes[order]); double leng = Convert.ToDouble(len); string[] sizes = { "Bytes", "KB", "MB", "GB", "TB" }; int order = 0; while (leng >= 1024 && order + 1 < sizes. Length) { order++; leng = leng / 1024; } return String.Format("{0:0.##} {1}", leng, sizes[order]); } |