无法计算我的计算器中的小数

时间:2015-03-17 14:58:12

标签: c# exception decimal calculator

所以,我在C#中制作了一个计算器,但它无法计算十进制数。 点击例如按钮时它完全正常:6然后。 (这是一个点)然后5.但是一旦我在表单中点击“+” - 按钮(或任何其他操作符),程序就会停止并且我收到一条消息说

  

“发生了'System.FormatException'类型的未处理异常   mscorlib.dll中。输入字符串的格式不正确“。

我不确切知道如何解决这个问题。有没有人知道如何解决这个问题?

这是我的代码:

namespace Kalkylator{
    public partial class Form1 : Form{
        String operation = ""; //the operation we will use
        Double resultat = 0; //the result we will get
        bool finished = false; //if false, we have not pressed the "=" button yet

        public Form1(){
            InitializeComponent();
        }

        //
        private void button_click(object sender, EventArgs e){
            if (finished == true){ //if we press any operator, clear the textbox-window so new numbers can be entered
                textBoxFonster.Clear(); 
            }
            finished = false; //we are not done with the calculation
            Button b = (Button)sender; 

            if (b.Text == "."){
                if (!textBoxFonster.Text.Contains(".")){
                    textBoxFonster.Text = textBoxFonster.Text + b.Text;
                }
            }
            else{
                textBoxFonster.Text = textBoxFonster.Text + b.Text; //writes the number in the textBox
            }
        }

        private void operator_click(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            operation = b.Text; //the operation we will perform is the operatorButton we will press
            resultat = Double.Parse(textBoxFonster.Text); //HERE IS WHERE THE PROGRAM GIVES ME THE ERROR.
            finished = true; //we are done with the calculation
        }

        private void clear_click(object sender, EventArgs e)
        {
            textBoxFonster.Text = ""; //clear the window from all text
            resultat = 0; //clear the value of resultat and set it to 0
        }

        private void LikaMed_click(object sender, EventArgs e)
        {
            switch(operation){
                case "+": //add the result with the text in the textBox
                    textBoxFonster.Text = (resultat + Double.Parse(textBoxFonster.Text)).ToString();
                    break;
                case "-": 
                    textBoxFonster.Text = (resultat - Double.Parse(textBoxFonster.Text)).ToString();
                    break;
                case "*": 
                    textBoxFonster.Text = (resultat * Double.Parse(textBoxFonster.Text)).ToString();
                    break;
                case "%":
                    textBoxFonster.Text = (resultat / Double.Parse(textBoxFonster.Text) * (resultat/100)).ToString();
                    break;
                case "^": 
                    textBoxFonster.Text = (Math.Pow(resultat, Double.Parse(textBoxFonster.Text))).ToString();
                    break;
                case "Log": //takes the 10th log of resultat
                    textBoxFonster.Text = (Math.Log10(resultat)).ToString();
                    break;
                case "Sqrt":
                    textBoxFonster.Text = (Math.Sqrt(resultat)).ToString();
                    break;
                case "/": //divide the result with the text in the textBox if that text is not 0. If so, show an error message
                    if ((Double.Parse(textBoxFonster.Text)) != 0){
                        textBoxFonster.Text = (resultat / Double.Parse(textBoxFonster.Text)).ToString();
                    }
                    else{ //show error in MessageBox
                        MessageBox.Show("Cannot divide by 0!");
                    }
                    break;
                default:
                    break;
            }
            finished = true; //this will clear the result textbox when clicking another number after the equal sign has been clicked
        }
    }
}

1 个答案:

答案 0 :(得分:1)

请勿在未指定文化的情况下使用Double.Parse。

变化:

switch(operation){
    case "+": //add the result with the text in the textBox
        textBoxFonster.Text = (resultat + Double.Parse(textBoxFonster.Text)).ToString();
        break;
    case "-": 
        textBoxFonster.Text = (resultat - Double.Parse(textBoxFonster.Text)).ToString();
        break;

为:

Double operand1=resultat;
Double operand2=0;
Double.TryParse(textBoxFonster.Text,NumberStyles.Float,CultureInfo.InvariantCulture,out operand2);
switch(operation){
    case "+": //add the result with the text in the textBox
        textBoxFonster.Text = (operand1 + operand2).ToString();
        break;
    case "-": 
        textBoxFonster.Text = (operand1 - operand2).ToString();
        break;

或者,您实际上可以支持多种文化,并更改此代码:

        if (b.Text == "."){
            if (!textBoxFonster.Text.Contains(".")){
                textBoxFonster.Text = textBoxFonster.Text + b.Text;
            }
        }

到此:

        if (b.Text == System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator){
            if (!textBoxFonster.Text.Contains(System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator)){
                textBoxFonster.Text = textBoxFonster.Text + b.Text;
            }
        }