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);
这是我目前的代码,它确实很有用。问题是,我需要将所有TextBox都变成货币格式。当我尝试数学不再有效。任何想法都会有所帮助我所拥有的最大问题是将其转换为货币时的销售税。如果小计是货币格式,我无法进行数学运算。我试图将它转换回十进制,但是当我这样做时,我无法运行命令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");
我的错误是FormatException在Convert.toInt16(xtextboxTotal.text)上未处理
答案 0 :(得分:0)
将数字字符串格式化为货币时,必须先将其解析,然后才能将其重新转换回数字格式,这取决于您首先用于创建数字的固有格式规则。
请参阅: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.