Added long extensions

This commit is contained in:
Thorsten Sommer 2025-01-23 12:57:49 +01:00
parent bf7cf131a1
commit fa849d39a9
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108

View File

@ -0,0 +1,22 @@
namespace AIStudio.Tools;
public static class LongExtensions
{
/// <summary>
/// Formats the file size in a human-readable format.
/// </summary>
/// <param name="sizeBytes">The size in bytes.</param>
/// <returns>The formatted file size.</returns>
public static string FileSize(this long sizeBytes)
{
string[] sizes = { "B", "kB", "MB", "GB", "TB" };
var order = 0;
while (sizeBytes >= 1024 && order < sizes.Length - 1)
{
order++;
sizeBytes /= 1024;
}
return $"{sizeBytes:0.##} {sizes[order]}";
}
}