【问题标题】:Cannot calculate decimals in my calculator无法在我的计算器中计算小数
【发布时间】:2015-03-17 14:58:11
【问题描述】:

所以,我用 C# 做了一个计算器,但它不能计算十进制数。 单击例如按钮时,它工作得非常好: 6 then 。 (这是一个点)然后是 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
        }
    }
}

【问题讨论】:

  • 猜测一下,您所在的语言环境通常使用, 来指定小数点吗?
  • 究竟在哪一行?您能否展示一个示例输入和文化设置?
  • @Phylogenesis 谢谢!你是绝对正确的。甚至没有想到它,现在它可以工作了:)
  • @chue 如果您找到了解决方案,您应该考虑自行回答您的问题。
  • @SonerGönül 一个例子:我点击了 6.5,然后点击了 + 操作符,在我点击操作符后程序给了我错误。但似乎系统发育是正确的。问题是我使用了. 而不是,

标签: c# exception decimal calculator


【解决方案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;
            }
        }

【讨论】:

  • 我能问你,为什么指定文化很重要?这是我第一次听说。
  • 因为没有它,您将使用系统默认文化。由于您的代码需要 .要在某些地方成为小数点分隔符,您应该在尝试将文本解析为数字时指定它。并非所有文化都使用 .指定小数点分隔符,并且如果您在具有默认区域性的机器上运行代码,即小数点分隔符为 ,千位分隔符为 .,则 double.Parse 将解析(1.002 为 1002 并将 1,002 解析为 1.002 等)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多