Combobox Value会员到货币

时间:2011-11-07 22:16:07

标签: c# winforms combobox

我有一个组合框,其中包含产品类型和相关价格。我需要取组合框的值成员作为价格并将其转换为将其与另一个值相乘,并能够将其作为货币放入发票中。目前我可以将它转换为字符串,但似乎我不能只使用.ToString(“C”)重载将其更改为货币。任何帮助都会很棒:

    private void btnAddProduct_Click(object sender, EventArgs e)
    {
        double invoiceTotal;
        double productTotal;
        double currentTotal;
        string multiplier;
        string price;

        //invoiceTotal = 0;
        price = Convert.ToString(comboBox1.SelectedValue);

        multiplier = comboBox2.Text;
        productTotal = Convert.ToDouble(txtProductTotal.Text);

        if (txtInvoiceTotal.Text != "")
        {
            invoiceTotal = Convert.ToDouble(txtInvoiceTotal.Text);
        }
        else
        {
            invoiceTotal = 0;
        }            
        currentTotal = productTotal + invoiceTotal;
        txtInvoiceTotal.Text = Convert.ToString(currentTotal);
        string prod = comboBox1.Text;

        if (txtExplanation.Text == "") 
        {
            txtExplanation.Text = prod + " X " + multiplier + " @ " + price;
        }
        else
            txtExplanation.Text = txtExplanation.Text + "\r\n" + prod + " X " + multiplier + " @ " + price;           

    }

2 个答案:

答案 0 :(得分:1)

comboBox1.SelectedValue应该已经是一个字符串。因此,您可能希望使用String.Format以您希望的方式对其进行格式化。否则,您可以使用Double.Parse(comboBox1.SelectedValue)将其解析为double,然后在double值上调用ToString(“C”)。

答案 1 :(得分:0)

您可能正在寻找的是字符串格式

var price = comboBox1.SelectedValue;
var currencyText = price.ToString("$#,##0.00");