繁体   English   中英

输入的字符串格式不正确,“ double.parse(textbox.text);”

[英]Input string was not in a correct format, “double.parse(textbox.text);”

您好,视觉工作室的新手。

到目前为止,这是我的代码,可以作为atm的示例,我有一个文本框,然后放入金额,然后在此按钮上单击了贷项,然后将金额添加到称为余额的标签中,并且我也有一个名为借方的按钮,可将钱从余额中扣除。 我正在WPF C#中执行此操作

到目前为止,我有这个。

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



    }
}

}

textboxamount.Text字符串不能解析为double。 为了避免异常,您可以改用double.TryParse

double amount;

if(double.TryParse(textboxamount.Text, out amount))
{
    totalamount += amount;
}

另外, label2似乎是Label ,您必须使用

label2.Text = balance1 + totalamount;

代替。

您不能相信用户在文本框中键入精确的double值。
如果输入不能转换为双精度,则Parse方法无法避免异常。
相反,使用double.TryParse方法可以测试键入的值是否实际上是一个double。 同样,您似乎正在使用货币值,因此使用十进制数据类型可能更好,并且在构建输出字符串时,请使用适当的格式来获取适合您的语言环境的货币字符串。 这也将避免在double / single数据类型中固有存在的舍入误差

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

问题是textboxamount.Text中的值包含一些不能转换为双textboxamount.Text的值。

处理此问题的最佳方法是使用double.TryParse代替:

private void buttoncredit_Click(object sender, RoutedEventArgs e)
{
    double newAmount;
    if(!double.TryParse(textboxamount.Text, out newAmount))
    {
        // The input is wrong - handle that
        MessageBox.Show("Please enter a valid amount");
        textboxamount.Focus();
        return;
    }

    totalamount += newAmount;
    balance1 = "Your Balance is: £";
    label2.Content = balance1 + totalamount;
    // .. Your code...

两个主要问题导致此错误:

  • 任何其他文本,例如空格或货币字符。
  • 不正确的本地化设置导致,. 被翻转。

可能包括,但我不记得是不是错误条件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM