简体   繁体   English

double.Parse中的输入字符串格式不正确

[英]Input string was not in a correct format in double.Parse

I am new to C#. 我是C#的新手。 I'm trying to make a calculator, but the following error occurred: 我正在尝试制作计算器,但发生了以下错误:

Input string was not in a correct format. 输入字符串的格式不正确。

This is the summary of the code: 这是代码的摘要:

double num1, num2, result;

private void button14_Click(object sender, EventArgs e)
{
    num1 = Convert.ToDouble(textBox1.Text);
    textBox1.Text = String.Empty;
    num2 = double.Parse(textBox1.Text);   **//ERROR OCCURED HERE**
    result = num1 - num2;
}

private void button13_Click(object sender, EventArgs e)
{
    num1 = Convert.ToDouble(textBox1.Text);
    textBox1.Text = String.Empty;
    num2 = System.Double.Parse(textBox1.Text);  **//ERROR OCCURED HERE**
    result = num1 + num2;
}

How to convert string to a double type? 如何将字符串转换为double类型?

Also remember that the Parse method is relying on the culture of your operating system to perform the conversion, so try to change your code to 还要记住,Parse方法依赖于操作系统的文化来执行转换,因此请尝试将代码更改为

num2 = double.Parse(textBox1.Text, CultureInfo.InvariantCulture);

You might also consider to use the 您也可以考虑使用

double.TryParse

method for better exception handling. 更好的异常处理方法。

What are you trying to achieve with this code? 你想用这段代码实现什么目标? It seems that your algorythm is wrong. 你的algorythm似乎是错误的。

Like others said, this code 像其他人说的,这段代码

textBox1.Text = String.Empty;
num2 = double.Parse(textBox1.Text);

will throw an Exception because an empty string cannot be converted to Double! 将抛出异常,因为空字符串无法转换为Double!

So, I'm wondering why did you reset your field. 所以,我想知道你为什么重置你的领域。 I thought about it for a while, and maybe I got what are you trying to do. 我想了一会儿,也许我得到了你想做的事情。 Let's say you type a number in TextBox1. 假设您在TextBox1中键入一个数字。 Then you press the "-" button to subtract and then you want to enter the second number to view the result. 然后按“ - ”按钮减去,然后输入第二个数字以查看结果。 Is this the case? 是这样的吗? If it is, the code you wrote is not going to wait for your next input! 如果是,您编写的代码不会等待您的下一次输入!

In fact, when you click the button, it just executes all the lines you wrote. 实际上,当您单击按钮时,它只会执行您编写的所有行。 I'd write something like this instead. 我会写这样的东西。

double num1, num2, result;
string operation;

private void button14_Click(object sender, EventArgs e) //Minus Button
{
    if (textBox1.Text != String.Empty) //Added if statement to see if the textBox is empty
        num1 = Convert.ToDouble(textBox1.Text);
    else
        num1 = 0; //If textBox is empty, set num1 to 0
    textBox1.Text = String.Empty;
    operation = "-";
}

private void button13_Click(object sender, EventArgs e) //Equals Button
{
    if (textBox1.Text != String.Empty)
        num2 = Convert.ToDouble(textBox1.Text);
    else
        num2 = 0;
    if (operation == "-")
    {
        result = num1 - num2;
        textBox1.Text = Convert.ToString(result);
    }
    if (operation == "+")
    {
        //You got it
    }
    //And so on...
}

EDIT: If the string is empty, this is going to always throw Exceptions, so I added a control. 编辑:如果字符串为空,这将始终抛出异常,所以我添加了一个控件。 If the string is empty, value becomes zero. 如果字符串为空,则值变为零。

Since you have cleared the textbox on the previous line, the Parse conversion fails. 由于您已清除上一行的文本框,因此Parse转换失败。

textBox1.Text = String.Empty;
num2 = double.Parse(textBox1.Text);

How will it convert String.Empty to Double? 它如何将String.Empty转换为Double? The way of doing it is not right. 这样做的方式是不对的。 For example, if the "+" button is clicked, you have to check whether there was already a number. 例如,如果单击“+”按钮,则必须检查是否已存在数字。 If so,, add the numbers and display the result: 如果是这样,添加数字并显示结果:

Double num;
private void Add_Click(object sender, EventArgs e)
{

   If (num != null)
   {
    num == num + Convert.ToDouble(textBox1.Text);
   }
   else
   {
    num1 == Convert.ToDouble(textBox1.Text);
   } 
   textBox1.Text = num;
}

Seems like num2 value should be fetched from textbox2 not textbox1 (You are setting textbox1.text to empty and trying to parse it to double again) 似乎应该从textbox2而不是textbox1获取num2值(您将textbox1.text设置为并尝试将其解析为再次加倍)

//You are setting textbox1 to empty
textBox1.Text = String.Empty;

//here trying to parse it to double
num2 = double.Parse(textBox1.Text);

Also don't use Convert.ToDouble(textBox1.Text) directly. 也不要直接使用Convert.ToDouble(textBox1.Text) if users type non numeric values your code will crash. 如果用户键入非数字值,您的代码将崩溃。 first check if its a valid number, always use doube.TryPrase() 首先检查它是否是有效数字,始终使用doube.TryPrase()

double num1;
double.TryParse(textBox1.Text, out num1);

Your code seems hard to understand whats the use of button_13 and button_14; 您的代码似乎很难理解使用button_13和button_14;

I will assume your trying to do this: 我会假设你试图这样做:

bool ifNew = true;
double num1 ,num2,result;
private void Add_Click(object sender, EventArgs e))
{
   if(ifNew)
   {

    num1 =  Convert.ToDouble(textBox1.Text);
    textbox1.Clear();
    ifNew = false;
    result += num1;

   }
   else
   {
    num2 = Convert.ToDouble(textBox1.Text);
    textbox1.Clear();
    result += num2;
    num1 = 0D;
    num2 = 0D;
    ifNew = true;
   } 


}

private void Equals_Click(object sender, EventArgs e)
{
   textboxl.Text = string.Format("{0:N}",result);
}

Or you could use Double.Parse in my Convert.ToDouble It depends on your operation but I am visualizing how to do add operation you can change and edit this depends on your operation 或者你可以在我的Convert.ToDouble中使用Double.Parse它取决于你的操作,但我可视化如何做添加操作你可以改变和编辑这取决于你的操作

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

相关问题 输入的字符串格式不正确,“ double.parse(textbox.text);” - Input string was not in a correct format, “double.parse(textbox.text);” 输入正确的Double.Parse(“ string”)FormatException - Double.Parse(“string”) FormatException with correct input double.parse System.FormatException: '输入字符串的格式不正确。' - double.parse System.FormatException: 'Input string was not in a correct format.' Double.Parse没有给出正确的结果 - Double.Parse not giving correct result 不能double.Parse字符串 - Can't double.Parse string 尝试将字符串解析为double,得到“输入字符串格式不正确”。 - Trying to parse a string to a double, getting “Input string was not in a correct format.” string.Format(…,double),然后是double.Parse,使用相同的NumberFormatInfo会导致FormatException。 为什么? - string.Format(…, double) followed by double.Parse using same NumberFormatInfo results in FormatException. Why? 设备语言不是英语Monodroid时,Double.Parse(string)无法正常工作 - Double.Parse(string) not working correct when device language is not english monodroid 输入的字符串格式不正确(双精度) - Input string was not in a correct format (double) 尝试double.Parse()时发生System.Format.FormatException - System.Format.FormatException when trying double.Parse()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM