Files
downkyicore/DownKyi.Core/Utils/Format.cs
to_mo_to e87b3eabe6 Update Format.cs
当合集中只有一个视频,会多出一个目录。返回空串拼接路径时可以忽略掉
2024-09-12 14:21:25 +08:00

140 lines
4.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)
{
var dur = duration / 60;
formatDuration = dur / 60 > 0 ? $"{dur / 60}h{dur % 60}m{duration % 60}s" : $"{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)
{
var dur = duration / 60;
formatDuration = dur / 60 > 0 ? $"{dur / 60:D2}:{dur % 60:D2}:{duration % 60:D2}" : $"00:{duration / 60:D2}:{duration % 60:D2}";
}
else
{
formatDuration = $"00:00:{duration:D2}";
}
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)
{
var dur = duration / 60;
formatDuration = dur / 60 > 0 ? $"{dur / 60:D2}:{dur % 60:D2}:{duration % 60:D2}" : $"{duration / 60:D2}:{duration % 60:D2}";
}
else
{
formatDuration = $"00:{duration:D2}";
}
return formatDuration;
}
/// <summary>
/// 格式化数字超过10000的数字将单位改为万超过100000000的数字将单位改为亿并保留1位小数
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public static string FormatNumber(long number)
{
return number switch
{
> 99999999 => (number / 100000000.0f).ToString("F1") + "亿",
> 9999 => (number / 10000.0f).ToString("F1") + "万",
_ => number.ToString()
};
}
/// <summary>
/// 格式化网速
/// </summary>
/// <param name="speed"></param>
/// <returns></returns>
public static string FormatSpeed(float speed)
{
string formatSpeed = speed switch
{
<= 0 => "0B/s",
< 1024 => $"{speed:F2}B/s",
< 1024 * 1024 => $"{speed / 1024:F2}KB/s",
_ => $"{speed / 1024 / 1024:F2}MB/s"
};
return formatSpeed;
}
/// <summary>
/// 格式化字节大小,可用于文件大小的显示
/// </summary>
/// <param name="fileSize"></param>
/// <returns></returns>
public static string FormatFileSize(long fileSize)
{
string formatFileSize = fileSize switch
{
<= 0 => "0B",
< 1024 => fileSize.ToString() + "B",
< 1024 * 1024 => (fileSize / 1024.0).ToString("#.##") + "KB",
< 1024 * 1024 * 1024 => (fileSize / 1024.0 / 1024.0).ToString("#.##") + "MB",
_ => (fileSize / 1024.0 / 1024.0 / 1024.0).ToString("#.##") + "GB"
};
return formatFileSize;
}
/// <summary>
/// 去除非法字符
/// </summary>
/// <param name="originName"></param>
/// <returns></returns>
public static string FormatFileName(string originName)
{
var destName = originName;
destName = Path.GetInvalidFileNameChars().Aggregate(destName, (current, c) => current.Replace(c.ToString(), string.Empty));
var cleanedName = destName
.SkipWhile(c => c == ' ' || c == '.')
.Reverse()
.SkipWhile(c => c == ' ' || c == '.')
.Reverse()
.ToArray();
return new string(cleanedName);
}
}