Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.Format in C# not returning modified int value

I am tring to return an int value with comma seperators within the value.

12345 would be returned as 12,345

The follwing code works:

int myInt = 1234567;
MessageBox.Show(string.Format("My number is {0}", myInt.ToString("#,#")));

12,345 is displayed as expected.

While the following code does no work, but from what I am reading, should work:

int myInt = 1234567;
MessageBox.Show(string.Format("My number is {0:#,#}", myInt.ToString()));

12345 is displayed.

Can you help me understand why the second set of code is not working?

Thanks

like image 255
Richard West Avatar asked Dec 03 '22 11:12

Richard West


1 Answers

You shouldn't ToString the int before the format. Try this:

MessageBox.Show(string.Format("My number is {0:#,#}", myInt));
like image 164
Lucero Avatar answered Dec 21 '22 12:12

Lucero