Solved

C# FormatString Thousands formatting

Posted on 2013-02-07
5
431 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

623 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