使用小数时得到 System.FormatException,好像我不应该
Getting a System.FormatException when using a decimal, it seems as though I shouldn't be
我需要制作一个 "banking" 程序,除了一个问题外,我已经设置好它并开始工作了。当我试图提取比帐户中更多的钱时,我不断收到 System.FormatException 告诉我 "input string was not in correct format"。我正在使用十进制变量,当我在文本框中输入 101 以从包含 100 美元的帐户中提取时,我得到了异常。
我想警告用户他们没有足够的资金,然后将账户金额恢复到他们试图取款之前的金额为负数。我该怎么办?我在很多地方都在网上看过,但在这件事上没有找到太多帮助。另外请对我放轻松,我对编程还是很陌生。
ps。当存款或取款不为负数时,这不是问题,只有当账户为负数时才会出现异常。
if (amount < 0m)
{
amount += Decimal.Parse(textBox1.Text); // this is where the error occurs
MessageBox.Show("invalid funds");
}
完整代码在这里
Decimal amount = 0.00m;
public Form1()
{
InitializeComponent();
}
private void Deposit_Click(object sender, EventArgs e)
{
try
{
//checks for a user input below or equal to 1000 and greater than 0
if (Decimal.Parse(textBox1.Text) <= 1000 && Decimal.Parse(textBox1.Text) > 0)
{
amount += Decimal.Parse(textBox1.Text);
textBox1.Text = "";
}
else
{
{
// shows when ever a user trys to deposit anything other than a valid number
MessageBox.Show("Please enter an amount between [=12=] - ,000.");
textBox1.Text = "";
}
}
}
// catches exceptions when the user trys to deposit anything other than a valid number
catch
{
MessageBox.Show("Please enter an amount between [=12=] - ,000.");
textBox1.Text = "";
}
}
private void Account_Click(object sender, EventArgs e)
{
textBox1.Text = string.Format("$" + amount.ToString());
}
private void Withdrawal_Click(object sender, EventArgs e)
{
try
{
//checks for a user input below or equal to 1000
if (Decimal.Parse(textBox1.Text) <= 1000 && Decimal.Parse(textBox1.Text) > 0)
{
amount -= Decimal.Parse(textBox1.Text);
textBox1.Text = "";
}
else
{
{
// shows when ever a user trys to withdrawal anything other than a valid number
MessageBox.Show("Please enter an amount between [=12=] - ,000.");
textBox1.Text = "";
}
}
}
// catches exceptions when the user trys to withdrawal anything other than a valid number
catch
{
MessageBox.Show("Please enter an amount between [=12=] - ,000.");
textBox1.Text = "";
}
//checks to see if the user has withdrawn the account below 0
if (amount < 0m)
{
amount += Decimal.Parse(textBox1.Text);
MessageBox.Show("invalid funds");
}
}
//clears the text box for a new entry when the user click on it
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.Text = "";
}
有时让 textBox1 输出美元符号 (Account_Click
) 可能对您没有帮助,但在其他时候它应该 而不是 有美元符号(只是数字在 Withdrawal_Click
).
这种事情搅乱了局面,不仅使应用程序的使用变得混乱,而且在调试它时也会使您感到困惑 - 也许在这种情况下不会,但在您编写更复杂的程序时。
出现异常是因为文本框中有一个字符对小数点无效(我猜是 $)。
你想要做的是当 Visual Studio 因异常而停止时,将鼠标悬停在 "textBox1.Text" 上并查看它的值是什么,它是否包含“$”或任何空格?
我需要制作一个 "banking" 程序,除了一个问题外,我已经设置好它并开始工作了。当我试图提取比帐户中更多的钱时,我不断收到 System.FormatException 告诉我 "input string was not in correct format"。我正在使用十进制变量,当我在文本框中输入 101 以从包含 100 美元的帐户中提取时,我得到了异常。
我想警告用户他们没有足够的资金,然后将账户金额恢复到他们试图取款之前的金额为负数。我该怎么办?我在很多地方都在网上看过,但在这件事上没有找到太多帮助。另外请对我放轻松,我对编程还是很陌生。
ps。当存款或取款不为负数时,这不是问题,只有当账户为负数时才会出现异常。
if (amount < 0m)
{
amount += Decimal.Parse(textBox1.Text); // this is where the error occurs
MessageBox.Show("invalid funds");
}
完整代码在这里
Decimal amount = 0.00m;
public Form1()
{
InitializeComponent();
}
private void Deposit_Click(object sender, EventArgs e)
{
try
{
//checks for a user input below or equal to 1000 and greater than 0
if (Decimal.Parse(textBox1.Text) <= 1000 && Decimal.Parse(textBox1.Text) > 0)
{
amount += Decimal.Parse(textBox1.Text);
textBox1.Text = "";
}
else
{
{
// shows when ever a user trys to deposit anything other than a valid number
MessageBox.Show("Please enter an amount between [=12=] - ,000.");
textBox1.Text = "";
}
}
}
// catches exceptions when the user trys to deposit anything other than a valid number
catch
{
MessageBox.Show("Please enter an amount between [=12=] - ,000.");
textBox1.Text = "";
}
}
private void Account_Click(object sender, EventArgs e)
{
textBox1.Text = string.Format("$" + amount.ToString());
}
private void Withdrawal_Click(object sender, EventArgs e)
{
try
{
//checks for a user input below or equal to 1000
if (Decimal.Parse(textBox1.Text) <= 1000 && Decimal.Parse(textBox1.Text) > 0)
{
amount -= Decimal.Parse(textBox1.Text);
textBox1.Text = "";
}
else
{
{
// shows when ever a user trys to withdrawal anything other than a valid number
MessageBox.Show("Please enter an amount between [=12=] - ,000.");
textBox1.Text = "";
}
}
}
// catches exceptions when the user trys to withdrawal anything other than a valid number
catch
{
MessageBox.Show("Please enter an amount between [=12=] - ,000.");
textBox1.Text = "";
}
//checks to see if the user has withdrawn the account below 0
if (amount < 0m)
{
amount += Decimal.Parse(textBox1.Text);
MessageBox.Show("invalid funds");
}
}
//clears the text box for a new entry when the user click on it
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.Text = "";
}
有时让 textBox1 输出美元符号 (Account_Click
) 可能对您没有帮助,但在其他时候它应该 而不是 有美元符号(只是数字在 Withdrawal_Click
).
这种事情搅乱了局面,不仅使应用程序的使用变得混乱,而且在调试它时也会使您感到困惑 - 也许在这种情况下不会,但在您编写更复杂的程序时。
出现异常是因为文本框中有一个字符对小数点无效(我猜是 $)。
你想要做的是当 Visual Studio 因异常而停止时,将鼠标悬停在 "textBox1.Text" 上并查看它的值是什么,它是否包含“$”或任何空格?