简单的收银程序
私人无效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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将数字字符串格式化为货币时,您必须先解析它,然后才能将其重新转换回数字形式,这取决于您最初用于创建数字的货币格式化规则。
请参阅:http://msdn.microsoft.com/en-us/library/3s27fasw .aspx
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