Created
February 21, 2018 15:49
-
-
Save glebov21/c882627408a3021ad937c676ddc00ca5 to your computer and use it in GitHub Desktop.
Number to Short string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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