Solved

C# FormatString Thousands formatting

Posted on 2013-02-07
5
426 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

Independent Software Vendors: 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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 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