Solved

C# FormatString Thousands formatting

Posted on 2013-02-07
5
421 Views
Last Modified: 2013-03-26
Hi,

I currently use the following FormatString to display Thousands as K.

"#,##0,K";

This works well to convert, say, 150000 to 150k, but I need to tweak it slightly to display a decimal of the hundreds. So, 1500 would be 1.5k and 5500 would be 5.5k.

Is there a FormatString to achieve this?

Thanks
0
Comment
Question by:wint100
5 Comments
 
LVL 23

Expert Comment

by:apresto
ID: 38863148
Good morning :)

Would this work for you:

http://stackoverflow.com/questions/2134161/format-number-like-stackoverflow-rounded-to-thousands-with-k-suffix

code extract:

static string FormatNumber(int num) {
    if (num >= 100000)
        return FormatNumber(num / 1000) + "K";
    if (num >= 10000) {
        return (num / 1000D).ToString("0.#") + "K";
    }
    return num.ToString("#,0");
}

Open in new window

0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 38863210
apresto 's solution work but he is exceedning a "Zero" (just for info)
static string FormatNumber(int num) {
    if (num >= 10000)  'one "0" less
        return FormatNumber(num / 1000) + "K";
    if (num >= 1000) {  'one "0" less
        return (num / 1000D).ToString("0.#") + "K";
    }
    return num.ToString("#,0");
}

Open in new window

0
 
LVL 1

Author Comment

by:wint100
ID: 38863221
That is looking good, but I need to use a Formatter as I am writing this to a Charts YAxis Label FormatString property, and the actual value must be of numeric Type to display on the Chart's YAxis.

Chart1.AxesY[0].Title = "kWh";
                        var maxDataValue = app.ElecAnnualData.Max(u => u._day_data);
                        if (maxDataValue > 1000)
                        {
                            Chart1.AxesY[0].ValueFormatString = "#,##0,K";
                            Chart1.AxesY[0].Title = "Thousands kWh";

                        }

Open in new window

0
 
LVL 1

Author Comment

by:wint100
ID: 38863293
The decimal can be there at all times, it doesn't hust have to be size dependant.

So:
1.5k
96.6k
150.1k
Etc..
0
 
LVL 4

Accepted Solution

by:
Srinivasulu Muppala earned 500 total points
ID: 38867439
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question