计算不正确
Calculations not correct
我在 Visual Studio 中用 C# 开发应用程序,按下按钮时计算不正确。让我解释一下,当数字为 5 5 5
时,结果为 70
,但当数字为 5.0 5.0 5
时,结果为 475
。它计算数字就像它们不是小数一样。如果有人可以帮助我,那就太好了。谢谢!
private void sum_Click(object sender, RoutedEventArgs e)
{
double n1;
double n2;
double n3;
if (double.TryParse(num1.Text.Replace(".", ","), out n1)
&& double.TryParse(num2.Text.Replace(".", ","), out n2)
&& double.TryParse(num3.Text.Replace(".", ","), out n3))
{
double sum = n1 * 4 + n2 * 5 + n3 * 5;
String m = Convert.ToString(sum);
sum1.Text = m;
}
else
{
sum1.Text = "Unesi sve!";
}
}
应该可以正常工作 double.TryParse() 处理逗号和句号:
private void sum_Click(object sender, RoutedEventArgs e)
{
double n1;
double n2;
double n3;
if (double.TryParse(num1.Text, out n1)
&& double.TryParse(num2.Text, out n2)
&& double.TryParse(num3.Text, out n3))
{
double sum = n1 * 4 + n2 * 5 + n3 * 5;
sum1.Text = sum.ToString();
}
else
{
sum1.Text = "Unesi sve!";
}
}
更新
Here 是一个使用字符串而不是文本框作为输入的小示例
您正在将 .
更改为 ,
。 ,
在您当前的 CultureInfo
中肯定是 NumberGroupSeparator
。因此 5,0
将被解析为 50
:
50 * 4 + 50 * 5 + 5 * 5 == 475
只要不替换 .
,您的代码就可以了:
if (double.TryParse(num1.Text, out n1) &&
double.TryParse(num2.Text, out n2) &&
double.TryParse(num3.Text, out n3)) ...
如果要使用 ,
作为小数点分隔符:
var culture = CultureInfo.CurrentCulture.Clone() as CultureInfo;
culture.NumberFormat.NumberDecimalSeparator = ",";
if (double.TryParse(num1.Text, NumberStyles.Float, culture, out n1) &&
double.TryParse(num2.Text, NumberStyles.Float, culture, out n2) &&
double.TryParse(num3.Text, NumberStyles.Float, culture, out n3)) ...
我在 Visual Studio 中用 C# 开发应用程序,按下按钮时计算不正确。让我解释一下,当数字为 5 5 5
时,结果为 70
,但当数字为 5.0 5.0 5
时,结果为 475
。它计算数字就像它们不是小数一样。如果有人可以帮助我,那就太好了。谢谢!
private void sum_Click(object sender, RoutedEventArgs e)
{
double n1;
double n2;
double n3;
if (double.TryParse(num1.Text.Replace(".", ","), out n1)
&& double.TryParse(num2.Text.Replace(".", ","), out n2)
&& double.TryParse(num3.Text.Replace(".", ","), out n3))
{
double sum = n1 * 4 + n2 * 5 + n3 * 5;
String m = Convert.ToString(sum);
sum1.Text = m;
}
else
{
sum1.Text = "Unesi sve!";
}
}
应该可以正常工作 double.TryParse() 处理逗号和句号:
private void sum_Click(object sender, RoutedEventArgs e)
{
double n1;
double n2;
double n3;
if (double.TryParse(num1.Text, out n1)
&& double.TryParse(num2.Text, out n2)
&& double.TryParse(num3.Text, out n3))
{
double sum = n1 * 4 + n2 * 5 + n3 * 5;
sum1.Text = sum.ToString();
}
else
{
sum1.Text = "Unesi sve!";
}
}
更新
Here 是一个使用字符串而不是文本框作为输入的小示例
您正在将 .
更改为 ,
。 ,
在您当前的 CultureInfo
中肯定是 NumberGroupSeparator
。因此 5,0
将被解析为 50
:
50 * 4 + 50 * 5 + 5 * 5 == 475
只要不替换 .
,您的代码就可以了:
if (double.TryParse(num1.Text, out n1) &&
double.TryParse(num2.Text, out n2) &&
double.TryParse(num3.Text, out n3)) ...
如果要使用 ,
作为小数点分隔符:
var culture = CultureInfo.CurrentCulture.Clone() as CultureInfo;
culture.NumberFormat.NumberDecimalSeparator = ",";
if (double.TryParse(num1.Text, NumberStyles.Float, culture, out n1) &&
double.TryParse(num2.Text, NumberStyles.Float, culture, out n2) &&
double.TryParse(num3.Text, NumberStyles.Float, culture, out n3)) ...