/// <summary> /// 人类可识别的文件大小显示格式 /// </summary> /// <param name="size">文件大小(Byte为单位)</param> /// <returns></returns> public static string HumanReadableFileSize(double size) { string[] units = new string[] { "B", "KB", "MB", "GB", "TB", "PB" }; double mod = 1024.0; int i = 0; while (size >= mod) { size /= mod; i++; } //四舍六入 //return Math.Round(size) + units[i]; //取小数点后一位 return size.ToString("0.0"); }
人类可识别的时间大小
/// <summary> /// 人类可识别的时间大小 /// </summary> /// <param name="seconds">总秒数</param> /// <returns></returns> private string GetHumanTime(int seconds) { TimeSpan ts = new TimeSpan(0, 0, seconds); System.Text.StringBuilder sb = new System.Text.StringBuilder(); if (ts.Days > 0) { sb.Append((int)ts.TotalDays + "天"); } if (ts.Hours > 0) { sb.Append(ts.Hours + "小时"); } if (ts.Minutes > 0) { sb.Append(ts.Minutes + "分"); } if (ts.Seconds > 0) { sb.Append(ts.Seconds + "秒"); } return sb.ToString(); }