Add download speed to NetworkSocket

This commit is contained in:
Stone_Red
2023-12-14 12:29:17 +01:00
parent 661a506e76
commit d383d4d15b
4 changed files with 44 additions and 35 deletions
@@ -0,0 +1,37 @@
namespace HyperbolicDownloaderApi.Utilities;
internal static class UnitFormatter
{
public static string TransferRate(long bytesPerSecond)
{
string[] ordinals = new[] { "", "K", "M", "G", "T", "P", "E" };
decimal rate = bytesPerSecond * 8;
int ordinal = 0;
while (rate > 1000)
{
rate /= 1000;
ordinal++;
}
return $"{Math.Round(rate, 0, MidpointRounding.AwayFromZero)}{ordinals[ordinal]}bps";
}
public static string FileSize(long bytes)
{
string[] ordinals = new[] { "", "K", "M", "G", "T", "P", "E" };
decimal rate = bytes;
int ordinal = 0;
while (rate > 1000)
{
rate /= 1000;
ordinal++;
}
return $"{Math.Round(rate, 0, MidpointRounding.AwayFromZero)}{ordinals[ordinal]}B";
}
}