Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# FormatString Thousands formatting

Posted on 2013-02-07
5
Medium Priority
?
442 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 2000 total points
ID: 38867439
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Loops Section Overview
Suggested Courses

877 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