The following code can be used to shorten a amount from 1 Trillion to 1T, 1 Billion to 1 B, 1 Million to 1M and 1 Thousand to 1K. You can customize it to accept more number of scales amount like Quadrillion, Quintillion, Sextillion, Septillion etc.
public static string GetFormattedAmountText(double amount)
{
var formattedAmount = string.Empty;
var x = amount; //the number to be evaluated
var e = 0; //to accumulate the scale
var i = x; //a working variable
while (i > 1)
{
i = i / 10;
e++;
}
if (e >= 12)
{
formattedAmount = x / 1E9 + "T";
}
else if (e >= 9)
{
formattedAmount = x / 1E9 + "B";
}
else if (e >= 6)
{
formattedAmount = x / 1E6 + "M";
}
else if (e >= 3)
{
formattedAmount = x / 1E3 + "K";
}
return formattedAmount;
}
No comments:
Post a Comment