mlaumcherb/Mlaumcherb.Client.Avalonia/сеть/DataSize.cs
2024-11-06 00:04:12 +05:00

24 lines
612 B
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

namespace Mlaumcherb.Client.Avalonia.сеть;
public record struct DataSize(long Bytes)
{
static string BytesToHumanReadable(long bytes)
{
long K = bytes / 1024;
if (K == 0)
return $"{bytes}b";
float M = K / 1024f;
if (M < 1)
return $"{K:N1}Kb";
float G = M / 1024f;
if (G < 1)
return $"{M:N1}Mb";
return $"{G:N1}Gb";
}
public override string ToString() => BytesToHumanReadable(Bytes);
public static implicit operator DataSize(long bytes) => new DataSize(bytes);
}