wabbajack/Wabbajack.RateLimiter/NumberExtensions.cs

24 lines
710 B
C#
Raw Normal View History

2021-09-27 12:42:46 +00:00
using System;
2021-10-23 16:51:17 +00:00
namespace Wabbajack.Common;
public static class NumberExtensions
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
private static readonly string[]
Suffix = {"B", "KB", "MB", "GB", "TB", "PB", "EB"}; // Longs run out around EB
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public static string ToFileSizeString(this long byteCount)
{
if (byteCount == 0)
return "0" + Suffix[0];
var bytes = Math.Abs(byteCount);
var place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
var num = Math.Round(bytes / Math.Pow(1024, place), 1);
return Math.Sign(byteCount) * num + Suffix[place];
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public static string ToFileSizeString(this int byteCount)
{
return ToFileSizeString((long) byteCount);
2021-09-27 12:42:46 +00:00
}
}