How can I format numbers in C# so 12523 becomes "12K", 2323542 becomes "2M", etc? [duplicate]
开发者_运维问答Possible Duplicate:
Format Number like StackoverFlow (rounded to thousands with K suffix)
How can I format numbers in C# so 12523.57 becomes "12K", 2323542.32 becomes "2M", etc?
I don't know how to append the correct number abbreviation (K, M, etc) and show the appropriate digits?
So,
1000 = 1K
2123.32 = 2K
30040 = 30k
2000000 = 2M
Is there a built in way in C# to do this?
I don't think this is standard functionality in C#/.Net, but it's not that difficult to do this yourself. In pseudocode it would be something like this:
if (number>1000000)
string = floor(number/1000000).ToString() + "M";
else if (number > 1000)
string = floor(number/1000).ToString() + "K";
else
string = number.ToString();
If you don't want to truncate, but round, use round instead of floor.
There's no built in way, you'll have to roll your own routine, similar to this:
public string ConvertNumber(int num)
{
if (num>= 1000)
return string.Concat(num/ 1000, "k");
else
return num.ToString();
}
精彩评论