You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
downkyi/DownKyi.Core/Utils/Format.cs

169 lines
5.2 KiB

namespace DownKyi.Core.Utils
{
public static class Format
{
/// <summary>
/// 格式化Duration时间
/// </summary>
/// <param name="duration"></param>
/// <returns></returns>
public static string FormatDuration(long duration)
{
string formatDuration;
if (duration / 60 > 0)
{
long dur = duration / 60;
if (dur / 60 > 0)
{
formatDuration = $"{dur / 60}h{dur % 60}m{duration % 60}s";
}
else
{
formatDuration = $"{duration / 60}m{duration % 60}s";
}
}
else
{
formatDuration = $"{duration}s";
}
return formatDuration;
}
/// <summary>
/// 格式化Duration时间格式为00:00:00
/// </summary>
/// <param name="duration"></param>
/// <returns></returns>
public static string FormatDuration2(long duration)
{
string formatDuration;
if (duration / 60 > 0)
{
long dur = duration / 60;
if (dur / 60 > 0)
{
formatDuration = string.Format("{0:D2}", dur / 60) + ":" + string.Format("{0:D2}", dur % 60) + ":" + string.Format("{0:D2}", duration % 60);
}
else
{
formatDuration = "00:" + string.Format("{0:D2}", duration / 60) + ":" + string.Format("{0:D2}", duration % 60);
}
}
else
{
formatDuration = "00:00:" + string.Format("{0:D2}", duration);
}
return formatDuration;
}
/// <summary>
/// 格式化Duration时间格式为00:00
/// </summary>
/// <param name="duration"></param>
/// <returns></returns>
public static string FormatDuration3(long duration)
{
string formatDuration;
if (duration / 60 > 0)
{
long dur = duration / 60;
if (dur / 60 > 0)
{
formatDuration = string.Format("{0:D2}", dur / 60) + ":" + string.Format("{0:D2}", dur % 60) + ":" + string.Format("{0:D2}", duration % 60);
}
else
{
formatDuration = string.Format("{0:D2}", duration / 60) + ":" + string.Format("{0:D2}", duration % 60);
}
}
else
{
formatDuration = "00:" + string.Format("{0:D2}", duration);
}
return formatDuration;
}
/// <summary>
/// 格式化数字超过10000的数字将单位改为万超过100000000的数字将单位改为亿并保留1位小数
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public static string FormatNumber(long number)
{
if (number > 99999999)
{
return (number / 100000000.0f).ToString("F1") + "亿";
}
if (number > 9999)
{
return (number / 10000.0f).ToString("F1") + "万";
}
else
{
return number.ToString();
}
}
/// <summary>
/// 格式化网速
/// </summary>
/// <param name="speed"></param>
/// <returns></returns>
public static string FormatSpeed(float speed)
{
string formatSpeed;
if (speed <= 0)
{
formatSpeed = "0B/s";
}
else if (speed < 1024)
{
formatSpeed = speed.ToString("#.##") + "B/s";
}
else if (speed < 1024 * 1024)
{
formatSpeed = (speed / 1024).ToString("#.##") + "KB/s";
}
else
{
formatSpeed = (speed / 1024 / 1024).ToString("#.##") + "MB/s";
}
return formatSpeed;
}
/// <summary>
/// 格式化字节大小,可用于文件大小的显示
/// </summary>
/// <param name="fileSize"></param>
/// <returns></returns>
public static string FormatFileSize(long fileSize)
{
string formatFileSize;
if (fileSize <= 0)
{
formatFileSize = "0B";
}
else if (fileSize < 1024)
{
formatFileSize = fileSize.ToString() + "B";
}
else if (fileSize < 1024 * 1024)
{
formatFileSize = (fileSize / 1024.0).ToString("#.##") + "KB";
}
else if (fileSize < 1024 * 1024 * 1024)
{
formatFileSize = (fileSize / 1024.0 / 1024.0).ToString("#.##") + "MB";
}
else
{
formatFileSize = (fileSize / 1024.0 / 1024.0 / 1024.0).ToString("#.##") + "GB";
}
return formatFileSize;
}
}
}