问题描述
您好,我是视觉工作室的新手.
Hi am quite new to visual studios.
到目前为止,这是我制作自动取款机示例的代码,我有一个文本框,我输入了一个金额,然后我有这个按钮,我点击信用,它将金额添加到一个名为余额的标签,我还有一个名为借记的按钮,可以从余额中取出钱.我在 wpf c# 中这样做
This is my code so far to make an example of an atm, i have a text box and i put an amount in and then i have this button where i click credit and it adds the amount to a label called balance and i also have a button named debit which takes the money away from the balance. I'm doing this in wpf c#
到目前为止,我有这个.
so far i have this.
namespace BankAccounts
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private double totalamount = 0;
public string balance1;
private void buttoncredit_Click(object sender, RoutedEventArgs e)
{
totalamount = totalamount + double.Parse(textboxamount.Text)
balance1 = "Your Balance is: £";
label2.Content = balance1 + totalamount;
textboxamount.Clear();
}
private void buttondebit_Click(object sender, RoutedEventArgs e)
{
if (totalamount - double.Parse(textboxamount.Text) < 0)
{
MessageBox.Show("Overdraft Limit is not set please contact Customer Services");
}
else
{
totalamount = totalamount - double.Parse(textboxamount.Text);
balance1 = " Your Balance is: £";
label2.Content = balance1 + totalamount;
textboxamount.Clear();
}
}
private void listboxtransactions_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
}
}
推荐答案
您不能相信您的用户会在您的文本框中准确键入一个双精度值.
如果输入无法转换为双精度,则 Parse 方法无法避免异常.
相反, double.TryParse 方法提供了测试的机会如果键入的值实际上是双精度值.此外,您似乎正在使用货币值,因此使用十进制数据类型可能会更好,并且在构建输出字符串时使用适当的格式来为您的语言环境获取正确的货币字符串.这也将避免双/单数据类型中固有的舍入错误
You can't trust your user to type exactly a double value in your textbox.
The Parse method cannot avoid an exception if the input cannot be converted to a double.
Instead the double.TryParse methods give the chance to test if the value typed is effectively a double. Also it seems that you are working with currency values, so perhaps it is better to use a decimal data type and when building the output string use the appropriate formatting to get a correct currency string for your locale. This will also avoid rounding errors intrinsically present in the double/single datatype
private decimal totalamount = 0;
public string balance1;
private void buttoncredit_Click(object sender, RoutedEventArgs e)
{
decimal temp;
if(decimal.TryParse(textboxamount.Text, out temp))
{
totalamount = totalamount + temp;
balance1 = "Your Balance is: ";
label2.Content = balance1 + totalamount.ToString("C");
textboxamount.Clear();
}
else
MessageBox.Show("Not a valid amount");
}
private void buttondebit_Click(object sender, RoutedEventArgs e)
{
decimal temp;
if(decimal.TryParse(textboxamount.Text, out temp))
{
if (totalamount - temp < 0)
{
MessageBox.Show("Overdraft Limit is not set please contact Customer Services");
}
else
{
totalamount = totalamount - temp;
balance1 = " Your Balance is: ";
label2.Content = balance1 + totalamount.ToString("C");
textboxamount.Clear();
}
}
else
MessageBox.Show("Not a valid amount");
}
这篇关于输入字符串的格式不正确,“double.parse(textbox.text);"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!