Struggling with a piece of code.

Hello all, I hope that it is allowed to make this post here. I am taking an intro to programming this semester at school. I am working on a project, and have been running into an issue with a specific piece of code and even my instructor wasn't quite sure why it was not working.

The piece of code that is not working correct is as follows: Console.WriteLine("You will pay {0:C}", federalTax + " in federal taxes.")

The issue is that anything I put in after federalTax makes the dollar sign go away, but displays the text. For example, this would display You will pay 80.00 in federal taxes.

If I remove the + " in federal taxes." it displays You will pay $80.00

Can anyone give me an explanation as to what I am doing wrong that makes it so that the text that follows negates the currency? Our solution to this was to make the following the code instead:

string strFederalTax = String.Format("{0:C}", federalTax); Console.WriteLine("You will pay " + strFederalTax + " in federal taxes.");

Is it simply not possible to do it without 2 lines of code?

Forgive me for asking what is probably a very dumb question to most of you, but I have only been using Visual Studio for roughly 1 week.

by The_Fatness via /r/csharp

Leave a Reply