Here's a simple approach that will handle negative numbers, and give you the correct suffix for thousands, millions and billions:
public static string Convert(double value)
{
double absValue = Math.Abs(value);
if (absValue >= 1000000000)
{
return string.Format("${0:F2}b", value / 1000000000);
}
if (absValue >= 1000000)
{
return string.Format("${0:F2}m", value / 1000000);
}
if (absValue >= 1000)
{
return string.Format("${0:F1}k", value / 1000);
}
return string.Format("${0:F0}", value);
}
If you need different numbers of decimal places for the different suffixes, adjust the number after the
F
in the format string, or use your own custom format string.
If you want a different set of suffixes, add or remove
if
blocks. Just make sure you test the values in descending order. :)