【问题标题】:Simple Cashier Program简单的收银员程序
【发布时间】:2011-10-23 03:19:03
【问题描述】:

private void XButtonExit_Click(object sender, EventArgs e) { //这将关闭程序 关(); }

    private void xButtonClear_Click(object sender, EventArgs e)
    {
        //This will clear all the Text Boxes
        xTextBoxQuantity.Clear();
        xTextBoxPrice.Clear();
        xTextBoxRecieved.Clear();
        xTextBoxSubtotal.Clear();
        xTextBoxTotal.Clear();
        xTextBoxReturn.Clear();

        //This will turn the Return box and Lable back to hidden
        xTextBoxReturn.Visible = false;
        xLableReturn.Visible = false;

    }

    private void XButtonBalance_Click(object sender, EventArgs e)
    {
        //This will make the Return box and Lable visable
        xLableReturn.Visible = true;
        xTextBoxReturn.Visible = true;

        //Take value from xTextBoxTotal and store it
        Double Total = Convert.ToDouble(xTextBoxTotal.Text);

       //Take value from xTextBoxRecieved and store it
        double Recieved = Convert.ToDouble(xTextBoxRecieved.Text);

       //Take value from xTextBoxTotal and subtract from amout recieved
        double Amount = Total - Recieved;

        //Take the Amount and store it in xTextBoxReturn
        xTextBoxReturn.Text = Convert.ToString(Amount);

        //Change color, Red for amount owed and green for amout to give back
        if (Amount < .01) xTextBoxReturn.BackColor = Color.Green;
        else xTextBoxReturn.BackColor = Color.Red;


    }

    private void XButtonTotal_Click(object sender, EventArgs e)
    {

        //Take value from xTextBoxQuantity and store it
        Double num1 = Convert.ToDouble(xTextBoxQuantity.Text);

        //Take value from xTextBoxPrice and store it
        Double num2 = Convert.ToDouble(xTextBoxPrice.Text);

        //Peform Muptlication and store it in xTextBoxSubtotal

        Double Subtotal = num1 * num2;
        xTextBoxSubtotal.Text = Convert.ToString(Subtotal);

        //Take the Subtotal and add a 6% sales tax and store it in xTextBoxTotal
        Double SalesTax = Subtotal * 1.06;
        xTextBoxTotal.Text = Convert.ToString(SalesTax); 

这是我当前的代码,它确实很好用。问题是,我需要将所有 TextBoxes 转换为货币格式。当我尝试数学不再有效。任何想法都会有所帮助。我遇到的最大问题是将其转换为货币时的销售税。如果小计采用货币格式,我无法进行数学运算。我试图将其转换回十进制,但是当我这样做时,我无法运行命令 Subtotal * 1.0

这是我改变的:

//从xTextBoxTotal中取值并存储

        Convert.ToInt16(xTextBoxTotal.Text);
        Double Total = Convert.ToDouble(xTextBoxTotal.Text); 

       //Take value from xTextBoxRecieved and store it
        double Recieved = Convert.ToDouble(xTextBoxRecieved.Text);

       //Take value from xTextBoxTotal and subtract from amout recieved
        double Amount = Total - Recieved;

        //Take the Amount and store it in xTextBoxReturn
        xTextBoxReturn.Text = Amount.ToString("C");

        //Change color, Red for amount owed and green for amout to give back
        if (Amount < .01) xTextBoxReturn.BackColor = Color.Green;
        else xTextBoxReturn.BackColor = Color.Red;


    }

    private void XButtonTotal_Click(object sender, EventArgs e)
    {

        //Take value from xTextBoxQuantity and store it
        Double num1 = Convert.ToDouble(xTextBoxQuantity.Text);

        //Take value from xTextBoxPrice and store it
        Double num2 = Convert.ToDouble(xTextBoxPrice.Text);

        //Peform Muptlication and store it in xTextBoxSubtotal

        Double Subtotal = num1 * num2;
        xTextBoxSubtotal.Text = Subtotal.ToString("C");

        //Take the Subtotal and add a 6% sales tax and store it in xTextBoxTotal
        Double SalesTax = Subtotal * 1.06;
        xTextBoxTotal.Text = SalesTax.ToString("C"); 

我的错误是在 Convert.toInt16(xtextboxTotal.text) 上未处理 FormatException

【问题讨论】:

  • 如果我更改 xTextBoxSubtotal.Text = subtotal.toString("C") 我无法让销售税部分发挥作用
  • 你能解释一下'让数学工作'吗?您如何在文本框中制作货币格式,您是否使用 ToString("") 方法?请编辑问题以显示不起作用的源代码。

标签: math currency


【解决方案1】:

将数字字符串格式化为货币时,您必须先将其解析回来,然后才能将其重新转换回数字形式,这取决于您最初用于创建数字的货币格式化规则。

见:http://msdn.microsoft.com/en-us/library/3s27fasw.aspx

string value;
NumberStyles style;
CultureInfo culture;
double number;

// Parse currency value using en-GB culture.
value = "£1,097.63";
style = NumberStyles.Number | NumberStyles.AllowCurrencySymbol;
culture = CultureInfo.CreateSpecificCulture("en-GB");
if (Double.TryParse(value, style, culture, out number))
   Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
   Console.WriteLine("Unable to convert '{0}'.", value);
// Displays: 
//       Converted '£1,097.63' to 1097.63.

【讨论】:

    猜你喜欢
    • 2019-01-08
    • 1970-01-01
    • 2014-10-28
    • 2018-03-28
    • 2018-06-17
    • 1970-01-01
    • 2022-11-18
    • 2017-04-03
    • 1970-01-01
    相关资源
    最近更新 更多