Can't get Total to appear in desired format

Posted on 2006-05-15
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
    LVL 4

    Assisted Solution

    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

    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");

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    In one of my recent projects, I was working with IP cameras, I need to take some pictures from the camera and do some processing on it. The first step, was to retrieve the image from camera into Image object. So that it can be displayed or …
    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    This video discusses moving either the default database or any database to a new volume.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    794 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now