0

Updating a windows form label with employee pay information. I need to format the output to display currency for money values.

I have tried following the instructions in my textbook for formatting using {0:C}.

// Output to label.
lblEmployees.Text += "Name: " + empList[x].name + " Gross Pay: {0:C}" + 
empList[x].grossP + " Social Security Withheld: " + empList[x].socialW
+ " Medicare Withheld: " + empList[x].medicareW + " State 
Income Tax Withheld: " + empList[x].stateW  + " Federal Income Tax Withheld: 
" + empList[x].federalW + " Net Pay: " + empList[x].netP + "\n";

No errors attempted format I showing up as part of the string.

devronn008
  • 11
  • 5
  • 1
    Possible duplicate of [String format currency](https://stackoverflow.com/questions/10416553/string-format-currency) – Tom Stein Sep 19 '19 at 19:40
  • _No errors attempted format I showing up as part of the string_ Huh? What does that mean?? – TaW Sep 20 '19 at 09:13

3 Answers3

1

You need to use the string.Format() function so it knows to perform the formatting. Your code sees the format as part of the string and not something to format.

string.Format("{0:C}", empList[x].grossP) ;

In addition, your concatenation code is very hard to read. You may want to consider other formatting options.

String interpolation.

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated

String format all the way and remove the manual concatenation.

https://learn.microsoft.com/en-us/dotnet/api/system.string.format?view=netframework-4.8

EDIT:

As this is homework, I don't believe I should provide a direct answer. That said, here's an example that should help explain what you need to do. Basically, you need to ensure the number of tokens specified in your string to format matches the number of arguments passed.

string firstArgForFormatString = "part 1";
int secondArgForFormatString = 50;
decimal thirdArgForFormatString = 35.2m;

lblSomeLabel.Text += string.Format("first value will replace next token {0} and another value {1} and yet another value to format {2:C}.",
     firstArgForFormatString, 
     secondArgForFormatString, 
     thirdArgForFormatString);
quaabaam
  • 1,808
  • 1
  • 7
  • 15
  • 2
    yeah string interpolation is a good idea. makes code a lot easier to read – Tom Stein Sep 19 '19 at 19:44
  • I try this and it saying there is an error: System.FormatException: 'Index (zero based) must be greater than or equal to zero and less than the size of the argument list.' – devronn008 Sep 19 '19 at 19:46
  • that error is not related to the format string - it means that you're iterating more times than there are items in the array. – Rufus L Sep 19 '19 at 19:51
  • why not use StringBuilder here instead of concatenation? – imnotaduck Sep 19 '19 at 19:59
  • @RufusL But the issue only happens when I have the string.format there. – devronn008 Sep 19 '19 at 20:11
  • @devronn008 please update your code to what you currently use. It might be you passing not enough arguments to the Format method. – Tom Stein Sep 19 '19 at 20:20
  • @devronn008 The 0 in the format token `{0:C}` represents the index number of the argument to apply at that format position. You need to ensure the numbers specified in your token match the number of args you specified. You really need to read the documentation on string.Format. – quaabaam Sep 19 '19 at 20:30
  • @devronn008 I've edited my answer with an example to help with the error you are seeing. – quaabaam Sep 19 '19 at 20:45
0

You need to call String.Format("{0:C}", value) on that specific string. Like:

string.Format("Gross Pay: {0:C}",empList[x].grossP);

Otherwise your code is interpredted as you want to print "{0:C}".

Tom Stein
  • 345
  • 2
  • 15
  • I try this and it saying there is an error: System.FormatException: 'Index (zero based) must be greater than or equal to zero and less than the size of the argument list.' – devronn008 Sep 19 '19 at 19:43
0

Nowadays the string.Format() can be simplyfied like so:

$"Gross Pay: {empList[x].grossP:C}";

The variable is placed inside and te :C formatting info is placed right behind it.

Stefan
  • 652
  • 5
  • 19