Anath3ma Anath3ma - 1 month ago 6
C# Question

Dollar Amounts in C#

I looked through some other posts but nothing seemed to help. So what I'm trying to get is a code that reads out the current balance, with the phrase in front of it, with a dollar amount. Instead of printing the dollar sign its printing

{0:C}
. Am I using
{0:C}
incorrectly?

namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
double TotalAmount;
TotalAmount = 300.7 + 75.60;
string YourBalance = "Your account currently contains this much money: {0:C} " + TotalAmount;
Console.WriteLine(YourBalance);
Console.ReadLine();
}
}
}

Answer
string YourBalance = 
    string.Format("Your account currently contains this much money: {0:C} ",TotalAmount);

or using string interpolation with C# 6.0+

string YourBalance = $"Your account currently contains this much money: {TotalAmount:C} ";
Comments