1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2024-11-24 04:42:56 +01:00
Minecraft-Phantom-Panel/Utils/Phantom.Utils.IO/FileSize.cs

27 lines
645 B
C#

namespace Phantom.Utils.IO;
public readonly record struct FileSize {
private const int Scale = 1024;
private static readonly string[] Units = {
"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"
};
public ulong Bytes { get; }
public FileSize(ulong bytes) {
Bytes = bytes;
}
public string ToHumanReadable(int decimalPlaces) {
int power = Bytes == 0L ? 0 : (int) Math.Log(Bytes, Scale);
int unit = power >= Units.Length ? Units.Length - 1 : power;
if (unit == 0) {
return Bytes + " B";
}
string format = "{0:n" + decimalPlaces + "} {1}";
return string.Format(format, Bytes / Math.Pow(Scale, unit), Units[unit]);
}
}