• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

Can't get Total to appear in desired format

I having a memory lapse I want the total to show with a decimal point and a dollar sign. But I keep messy getting just numbers. Here is my code:

private void btnAdd_Click(object sender, System.EventArgs e)
                  double price1;
                  double price2;
                  double amount;

                  price1 = double.Parse(txtPrice1.Text);
                  price2 = double.Parse(txtPrice2.Text);
                  amount = price1 + price2;

                  lblTotalAmount.Text = amount.ToString();
2 Solutions
double price1;
double price2;
double amount;

price1 = Convert.ToDouble(txtPrice1.Text);
price2 = Convert.ToDouble(txtPrice2.Text);
amount = price1 + price2;

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us"); //set the 'locale' or format of your currency
lblTotalAmount.Text = amount.ToString("C"); //set currency format on returned string
Or if you want more control over the format than the basic currency formatter then

lblTotalAmount.Text = amount.ToString("$0.00");

Where the $ will actually be the character and any 0 will force at least one digit.  Therefore if you have an accounitng application or something like a gas station where you want the price out further than only the standard 2 decimals you could do this to get 4 decimal places:

lblTotalAmount.Text = amount.ToString("$0.0000");
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now