C# 我的计算器不能计算小数
因此,我用C#制作了一个计算器,但它不能计算十进制数。 当点击例如按钮时,它工作得非常好:6然后。(这是一个点)然后5。但当我在表单中单击“+”按钮(或任何其他操作符)后,程序就会停止,我会收到一条消息说 “在中发生类型为'System.FormatException'的未处理异常。” mscorlib.dll。输入字符串的格式不正确” 我不知道怎么解决这个问题。有人知道如何解决这个问题吗 这是我的密码:C# 我的计算器不能计算小数,c#,exception,decimal,calculator,C#,Exception,Decimal,Calculator,因此,我用C#制作了一个计算器,但它不能计算十进制数。 当点击例如按钮时,它工作得非常好:6然后。(这是一个点)然后5。但当我在表单中单击“+”按钮(或任何其他操作符)后,程序就会停止,我会收到一条消息说 “在中发生类型为'System.FormatException'的未处理异常。” mscorlib.dll。输入字符串的格式不正确” 我不知道怎么解决这个问题。有人知道如何解决这个问题吗 这是我的密码: namespace Kalkylator{ public partial clas
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
}
}
}
不要在不指定区域性的情况下使用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;
致:
或者,您可以实际支持多种区域性,并更改此代码:
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;
}
}
猜猜看,您所在的语言环境是否通常使用
,
来指定小数点?具体在哪一行?你能展示一个输入和区域设置的例子吗?@Phylogenesis谢谢!你完全正确。如果你找到了一个解决方案,你应该考虑回答你的问题。@ Suneg Onl胡:一个例子:我点击了6.5,然后是+运算符,程序在我点击了操作符之后就给了我错误。但似乎系统发育是正确的。问题是我使用了
而不是,
我可以问你,为什么指定文化很重要?这是我第一次听说它。因为没有它,你将使用系统默认文化。因为您的代码需要。要在某些位置用作小数分隔符,在尝试将文本解析为数字时,应指定该分隔符。并非所有文化都使用。若要指定十进制分隔符,并且如果在具有默认区域性(十进制分隔符为,千位分隔符为)的计算机上运行代码,则double.Parse将解析(1.002为1002,解析1002为1.002等)
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;
}
}