Solved

C# FormatString Thousands formatting

Posted on 2013-02-07
5
414 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now