简单的收银程序

发布于 2024-12-11 06:43:56 字数 3775 浏览 0 评论 0原文

私人无效XButtonExit_Click(对象发送者,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); 

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

这就是我更改的内容:

//Take value from xTextBoxTotal and store it

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

我的错误是 FormatException was unhandled on the Convert.toInt16( xtextboxTotal.text)

private void XButtonExit_Click(object sender, EventArgs e)
{
//This will close the program
Close();
}

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

This is my current code and it does work great. The issue is, I need to make all of the TextBoxes into a currency format. When I try the math no longer works. Any ideas will help. The biggest problem Im' having is with the sales tax when converting it to currency. I am unable to make the math work if subtotal is in the currency format. I tried to convert it back to decimal, but when I do I can't run the command Subtotal * 1.0

This is what I changed:

//Take value from xTextBoxTotal and store it

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

My error is FormatException was unhandled on the Convert.toInt16(xtextboxTotal.text)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇女薄笑 2024-12-18 06:43:56

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

请参阅: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.

When formating a numeric string as currency, you have to parse it back before you can re-convert it back to a numeric form, this depends on the curency formatting rules you used to create the number in the first place.

See: 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文