Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Number format question 1

Status
Not open for further replies.

RiverGuy

Programmer
Jul 18, 2002
5,011
US
I don't work with webforms very often, so forgive my ignorance. I'm using a chart, and I want to format my Y-axis in the thousands with a K. For example, 300,000 would be 300K. Alternatively, I would just use 300 and leave the K off. At the present, my format string is the following:
"{0:0,0}"

Thanks
 
formatting a string is just text. whether it is a webform, winform, or console, it's just a string.

here are some resources on string formatting. most of the information is repeated

I quickly scanned the links and it doesn't look like the format you are looking for doesn't exist, so you will need to parse/format the string yourself. maybe I missed it. if not something like this should work
Code:
public static class NumberFormatter
{
   public static string Format(this int i)
   {
      var s = i.ToString("#,0");
      var separators = s
         .ToCharArray()
         .Where(c => c == ',')
         .Count();
      var suffix = separators == 1 
         ? "K" 
         : separators == 2 
            ? "M" : "T";
      return s.Split(',')[0] + suffix;
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Thanks. I don't see it either. The problem is that the property of this method takes a format as an argument, so I cannot prepare it myself. Normally, I would just do something like (MyDecimal / 1000).ToString("0") & "K" but I've got no option to do so. I haven't worked much with the whole curly bracket format notation, so that's part of what was confusing. But reading further, it's just notation to specify which variable to format in a list. I was mistakenly under the impression the 0 had something to do with the format itself in web/html stuff.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top