Skip to content

Instantly share code, notes, and snippets.

@glebov21
Created February 21, 2018 15:49
Show Gist options
  • Save glebov21/c882627408a3021ad937c676ddc00ca5 to your computer and use it in GitHub Desktop.
Save glebov21/c882627408a3021ad937c676ddc00ca5 to your computer and use it in GitHub Desktop.
Number to Short string
public static string NumToShortStr(this double num)
{
string prefix = string.Empty;
if (num < 0)
prefix = "-";
num = Math.Abs(num);
if (num >= 1000000000000D)
return prefix + (num / 1000000000000D).ToString("0.#").Replace(',', '.') + "T";
if (num >= 1000000000D)
return prefix + (num / 1000000000D).ToString("0.#").Replace(',', '.') + "B";
if (num >= 1000000D)
return prefix + (num / 1000000D).ToString("0.#").Replace(',', '.') + "M";
if (num >= 1000D)
return prefix + (num / 1000D).ToString("0.#").Replace(',', '.') + "K";
if (num < 1D)
return prefix + num.ToString("0.##").Replace(',', '.');
return prefix + num.ToString("0.#").Replace(',', '.');
}
public static string NumToShortStr(this int num)
{
return NumToShortStr((double)num);
}
public static string NumToShortStr(this long num)
{
return NumToShortStr((double)num);
}
public static string NumToShortStr(this float num)
{
return NumToShortStr((double)num);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment