[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Can't get Total to appear in desired format

Posted on 2006-05-15
Medium Priority
Last Modified: 2012-05-05
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();
Question by:augie0216

Assisted Solution

SmithJW earned 100 total points
ID: 16687865
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
LVL 35

Accepted Solution

mrichmon earned 100 total points
ID: 16692316
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");

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Integration Management Part 2
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question