2

in my application i am update my ui with my label and i want to show the number in #,##0 format. myClass.getNumberOfFiles return string.

myLabel.Text = myClass.getNumberOfFiles();
user1710944
  • 1,419
  • 4
  • 16
  • 17
  • 3
    Why would a method that returns a number use a string type? fix it at that level. – Joel Coehoorn Oct 11 '12 at 21:56
  • In any case he will still have to convert the numeric type to a string down the line so that he can format it. – Jordan Kaye Oct 11 '12 at 21:56
  • I presume you are wanting to add a thousands separator. If so, see this question: http://stackoverflow.com/questions/105770/net-string-format-to-add-commas-in-thousands-place-for-a-number – nick_w Oct 11 '12 at 21:58

3 Answers3

7

Assuming getNumberOfFiles returns a string (which, by its name, it shouldn't) :

myLabel.Text = int.Parse(myClass.getNumberOfFiles()).ToString("#,##0");
H H
  • 263,252
  • 30
  • 330
  • 514
5

I suspect you want the standard "numeric" format specifier, with a precision of 0:

label.Text = GetNumberOfFiles().ToString("N0");

This is after you've fixed your getNumberOfFiles() method to be GetNumberOfFiles() (naming convention) and made it return int or long (a method which is meant to fetch a number should not return a string).

This will use the appropriate grouping for the current culture; if you want a different culture you can specify it as a second argument.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Error 4 Argument 1: cannot convert from 'string' to 'System.IFormatProvider' c – user1710944 Oct 11 '12 at 22:02
  • @user1710944: Did you read the bit where I said you needed to fix your method to return `int` or `long` first? Why on earth would you want a method called `GetNumberOfFiles` to return a string? – Jon Skeet Oct 11 '12 at 22:02
  • Little side-note: `"#,##0"` correctly deals with millions and trillions too. – H H Oct 11 '12 at 22:07
  • @HenkHolterman: Gosh, interesting - and frankly somewhat unexpected. It looks like you can actually get away with just `"#,0"`. – Jon Skeet Oct 11 '12 at 22:13
  • Yes, the extra `##` is for readability. The grouping-by-3 is hard code afaik. – H H Oct 11 '12 at 22:17
  • @HenkHolterman: Does it not use `NumberFormatInfo.NumberGroupSizes`? That's what I'd have expected. I view "#,##0" as *less* readable, as it implies to me a pattern which then *wouldn't* include extra grouping separators - hence my original comment. Of course, once you already know that it puts in as many separators as you need, that's a different matter :) – Jon Skeet Oct 11 '12 at 22:19
  • Never noticed that one. I guess all cultures (that I know of) have it hard-wired at 3. But I stand by the readability claim. Sometimes conveying a clear message is different from a totally formally correct one. – H H Oct 11 '12 at 22:35
  • @HenkHolterman: I guess my argument against the readability claim is my personal one - my guess as to what would happen was incorrect :) – Jon Skeet Oct 12 '12 at 05:47
4
int files;
if (int.TryParse(myClass.getNumberOfFiles(), out files)) {
    myLabel.Text = files.ToString("N0");
}

This won't work if you have any formatting in the number already I think. It will work though if on the return of getNumberOfFiles() someone was turning an int into a string. If getNumberOfFiles() is returning a formatted string, you will need to do some different stuff. Below assumes the formatting is in the en-US format coming in and you want to display it in Brazilian Portuguese for example. It is shown in a verbose manner so you know how to plug other cultures in if you need to. If its formatted and doesn't need to change between cultures I don't know why you couldn't just assign the return of getNumberOfFiles() directly to the label's Text property.

int files;
var incomingCulture = CultureInfo.CreateSpecificCulture("en-US");
var outgoingCulture = CultureInfo.CreateSpecificCulture("pt-BR");
if (int.TryParse(myClass.getNumberOfFiles(), NumberStyles.Number, incomingCulture, out files)) {
    myLabel.Text = files.ToString("N0", outgoingCulture);
}

That being said I agree with all the others saying it is ridiculous to return a string for an integer. But I know sometimes you don't have the luxury of being able to change it.

I'll also point out that if you use the named format specifiers like "N0", one day a programmer coming behind you will bless you in their heart when they have to globalize your code. This is because every CultureInfo instance has an implementation for each of the named formats, however it is impossible for it to have implementations for custom format specifiers.

Rob
  • 517
  • 3
  • 10