You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kev/Drawer/AI/Utils/FileUnit.cs

37 lines
952 B
C#

1 month ago
namespace AI.Utils
{
/// <summary>
/// 文件工具类
/// </summary>
public static class FileUnit
{
private static readonly string[] SizeUnits =
["B", "KB", "MB", "GB", "TB", "PB"];
/// <summary>
/// 格式化字节数为更易读的字符串格式
/// </summary>
/// <param name="bytes">字符数</param>
/// <returns>易读的字符串格式</returns>
public static string FormatFileSize(long bytes)
{
int unitIndex = 0;
decimal size = bytes;
// 主要是防止 UI 层可能没有做校验这些
if (size < 0)
{
return "0 B";
}
while (size >= 1024 && unitIndex < SizeUnits.Length - 1)
{
size /= 1024;
unitIndex++;
}
return $"{size:F1} {SizeUnits[unitIndex]}";
}
}
}