2024-04-23 07:10:10 +00:00
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
|
|
|
|
namespace Utils
|
2023-06-07 07:32:56 +00:00
|
|
|
|
{
|
|
|
|
|
public static class Formatter
|
|
|
|
|
{
|
|
|
|
|
private static readonly string[] sizeSuffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB" };
|
|
|
|
|
|
|
|
|
|
public static string FormatByteSize(long bytes)
|
|
|
|
|
{
|
|
|
|
|
if (bytes == 0) return "0" + sizeSuffixes[0];
|
|
|
|
|
|
|
|
|
|
var sizeOrder = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
|
|
|
|
|
var digit = Math.Round(bytes / Math.Pow(1024, sizeOrder), 1);
|
2024-04-23 07:10:10 +00:00
|
|
|
|
return digit.ToString(CultureInfo.InvariantCulture) + sizeSuffixes[sizeOrder];
|
2023-06-07 07:32:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|