I have a requirement to format large numbers like 4,316,000 as \"4.3m\".
How can I do this in C#?
long valueToFormat = 4316000;
var dict = new Dictionary() {
{1000000000, "b"},
{1000000, "m"},
{1000, "k"}
};
string formattedValue = valueToFormat.ToString();
foreach (long n in dict.Keys.OrderBy(k => k)) {
if (valueToFormat < n) {
continue;
}
double value = Math.Round(valueToFormat / (double)n, 1);
formattedValue = String.Format("{0}{1}", value, dict[n]);
}
Console.WriteLine(formattedValue);