I have a basic understanding of C# and the .NET Framework, I have been given an assignment to build a POS (Point Of Sales) screen, I have currently hit a small brick wall trying to convert a currency related string back to a double.

I have two list boxes and several product buttons on the screen, the buttons are populated using a library class provided to us (essentially showing we can work with components)

One list box holds the product name while the other holds the price of that product, when a product button is selected it takes the product name from the buttons text and within its tag there is the price which is added to the list box of prices.

my problem is I want to show the prices in the List Box as a currency also that it shows all '0' I can do this no problem by doing either the following

value.ToString("C");
string.Format("{0:C}",value);

or using Convert etc.

Although because I have done this if I want to remove an item from the list by double clicking I need to take away the price from the total so I need to convert to back to double although because its in its current format I get an error when trying to perform that action I have looked around and I cannot seem to find anyway around it, the only option I can see is just leaving the string value as it is and not convert it to a currency format.

the ERROR: {"Input string was not in a correct format."}

Code Snippet

 private void panelBtns_Click(object sender, EventArgs e)
    {
        Button panelBtn = (Button)sender;

        lstProduct.Items.Add(panelBtn.Text);

        double price = Convert.ToDouble(panelBtn.Tag);

        >>CURRENCY FORMAT>> lstPrice.Items.Add(string.Format("{0:C}",price));

        dblTotal = dblTotal + Convert.ToDouble(panelBtn.Tag);

        lblTotal.Text = string.Format("{0:C}", dblTotal);

        lblOutput.Text = "0";

        lblOutput.Tag = "0";
    }//End Panel Buttons 



 private void lstProduct_DoubleClick(object sender, EventArgs e)
    {
        int index = lstProduct.SelectedIndex;

        lstPrice.SelectedIndex = lstProduct.SelectedIndex ;

       >> ERROR HERE >> double price = Convert.ToDouble(lstPrice.GetItemText(lstPrice.SelectedItem));

        dblTotal = dblTotal - price; 

        lstProduct.Items.RemoveAt(index);

        lstPrice.Items.RemoveAt(index);

        lblTotal.Text = string.Format("{0:C}", dblTotal);

    }

Would anyone have any idea how I could possibly fix this, I had though about creating an invisible list to store the actual value of the tag so I can use that for later but would there be any other methods?

NOTE: I am also aware that using double for currency is not a very reliable

有帮助吗?

解决方案

The easiest way to parse the C format is probably with

Double.Parse(text, System.Globalization.NumberStyles.Currency)

Of course you would always want to use Decimal to handle currency, and Decimal.Parse takes the same parameters.

In this case, though, you would want to store your internal numeric values along with their textual representation rather than converting to a string and then parsing back into a number.

其他提示

other way, but please note, that you must try all cultures if removing symbol gives just decimal string, use Gabe's answer (posted just before mine :D)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Globalization;
using System.Threading;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            //US-en culture / default I'm developing on
            string currencyString = "$12.99"; //assuming got from: lstPrice.GetItemText(lstPrice.SelectedItem);
            CultureInfo ci = Thread.CurrentThread.CurrentUICulture;
            double d = Convert.ToDouble(currencyString.Replace(ci.NumberFormat.CurrencySymbol, ""));

            //using custom culture
            currencyString = "12.99zł";
            ci = CultureInfo.GetCultureInfo("PL-pl");
            d = Convert.ToDouble(currencyString.Replace(ci.NumberFormat.CurrencySymbol, ""));

        }
    }
}

You may want to look at assigning the decimal too with Math.Round Method

https://msdn.microsoft.com/en-us/library/system.math.round(v=vs.110).aspx

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top