rdt-client/server/RdtClient.Service/Helpers/FileSizeHelper.cs
2026-02-23 20:04:53 -07:00

28 lines
558 B
C#

namespace RdtClient.Service.Helpers;
public static class FileSizeHelper
{
public static String FormatSize(Int64? bytes)
{
if (bytes == null)
{
return "0 B";
}
String[] units =
{
"B", "KB", "MB", "GB", "TB", "PB", "EB"
};
var unitIndex = 0;
Double size = bytes.Value;
while (size >= 1024 && unitIndex < units.Length - 1)
{
size /= 1024;
unitIndex++;
}
return $"{size:0.##} {units[unitIndex]}";
}
}