33 lines
1.5 KiB
C#
33 lines
1.5 KiB
C#
using System.Globalization;
|
|||
|
|
|
||
|
|
namespace decePubClient.Extensions;
|
||
|
|
|
||
|
|
public static class NumbersExtensions
|
||
|
|
{
|
||
|
|
public static string ToDefaultCount(this int value) => value.ToString("N0", CultureInfo.CurrentCulture);
|
||
|
|
|
||
|
|
public static string ToDefaultCount(this long value) => value.ToString("N0", CultureInfo.CurrentCulture);
|
||
|
|
|
||
|
|
public static string ToDefaultCount(this double value) => value.ToString("N0", CultureInfo.CurrentCulture);
|
||
|
|
|
||
|
|
public static string Compact(this int count) => count switch
|
||
|
|
{
|
||
|
|
< 1000 => count.ToString(CultureInfo.CurrentCulture),
|
||
|
|
< 10_000 => (count / 1000d).ToString("0.#", CultureInfo.CurrentCulture) + "k",
|
||
|
|
< 1_000_000 => (count / 1000).ToString(CultureInfo.CurrentCulture) + "k",
|
||
|
|
_ => (count / 1_000_000d).ToString("0.#", CultureInfo.CurrentCulture) + "M"
|
||
|
|
};
|
||
|
|
|
||
|
|
public static string ToDefaultPercent(this double percent) => percent.ToString("0.#", CultureInfo.CurrentCulture) + "%";
|
||
|
|
|
||
|
|
public static string ToWholePercent(this double percent) => percent.ToString("0", CultureInfo.CurrentCulture) + "%";
|
||
|
|
|
||
|
|
public static string ToSignedPercent(this double percent) => percent.ToString("+0.0;-0.0;0.0", CultureInfo.InvariantCulture) + "%";
|
||
|
|
|
||
|
|
public static string ToCssNumber(this double value) => value.ToString("0.#", CultureInfo.InvariantCulture);
|
||
|
|
|
||
|
|
public static string ToReadout(this double value) => value.ToString("0.###", CultureInfo.InvariantCulture);
|
||
|
|
|
||
|
|
public static string ToFixed(this double value, int decimals) =>
|
||
|
|
value.ToString("F" + decimals.ToString(CultureInfo.InvariantCulture), CultureInfo.InvariantCulture);
|
||
|
|
}
|