How can I format numbers in C #, so 12523 becomes β€œ12K”, 2323542 becomes β€œ2M”, etc.?

Possible duplicate:
stack overflow

How can I format numbers in C #, so 12523.57 becomes "12K", 2323542.32 becomes "2M", etc.?

I do not know how to add the correct numerical abbreviation (K, M, etc.) and show the corresponding numbers?

So,

1000 = 1K 2123.32 = 2K 30040 = 30k 2000000 = 2M 

Is there a built-in way in C # for this?

+6
c # number-formatting
source share
2 answers

I don't think this is standard functionality in C # /. Net, but it’s not so difficult to do it yourself. In pseudo code, it will 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 do not want to truncate, but round, use a circle instead of the floor.

+6
source share

There is no built-in way, you will have to roll up your own procedure similar to this:

 public string ConvertNumber(int num) { if (num>= 1000) return string.Concat(num/ 1000, "k"); else return num.ToString(); } 
0
source share

All Articles