Updated Common files

This commit is contained in:
Toastie 2024-07-15 15:45:55 +12:00
parent 624171c35f
commit 81ebad702b
Signed by: toastie_t0ast
GPG key ID: 27F3B6855AFD40A4
2 changed files with 31 additions and 7 deletions

View file

@ -147,4 +147,5 @@ public static class StringExtensions
var newString = str.UnescapeUnicodeCodePoint(); var newString = str.UnescapeUnicodeCodePoint();
return newString; return newString;
}); });
} }

View file

@ -1,7 +1,30 @@
using System.Globalization;
namespace EllieBot.Extensions; namespace EllieBot.Extensions;
public static class NumberExtensions public static class NumberExtensions
{ {
public static DateTimeOffset ToUnixTimestamp(this double number) public static DateTimeOffset ToUnixTimestamp(this double number)
=> new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero).AddSeconds(number); => new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero).AddSeconds(number);
public static string ToShortString(this decimal value)
{
if (value <= 1_000)
return Math.Round(value, 2).ToString(CultureInfo.InvariantCulture);
if (value <= 1_000_000)
return Math.Round(value, 1).ToString(CultureInfo.InvariantCulture);
var tokens = " MBtq";
var i = 2;
while (true)
{
var num = (decimal)Math.Pow(1000, i);
if (num > value)
{
var num2 = (decimal)Math.Pow(1000, i - 1);
return $"{Math.Round((value / num2), 1)}{tokens[i - 1]}".Trim();
}
i++;
}
}
} }