Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我的总值不像计算器那样精确_C# - Fatal编程技术网

C# 我的总值不像计算器那样精确

C# 我的总值不像计算器那样精确,c#,C#,我表格上计算的总价值是错误的10800-10417*0.20=76.6是正确的值,但我的表单中的响应是8716.6?我做错了什么 我的表单总计是8716.60,而计算器上显示的正确值是76.6 public void wwtax() { double tax = 0; double value = 0; if (tax < 10417) { tax = Conver

我表格上计算的总价值是错误的
10800-10417*0.20=76.6是正确的值,但我的表单中的响应是
8716.6
?我做错了什么

我的表单总计是
8716.60
,而计算器上显示的正确值是
76.6

    public void wwtax()
    {
        double tax = 0;          
        double value = 0;

        if (tax < 10417)
        {
            tax = Convert.ToInt32(label21.Text);//the 10800 value
            label20.Text = "0";
        }
        if (tax > 10417)
        {
            tax = Convert.ToInt32(label21.Text);
            value = tax - 10417 * 0.20;
        }
        if (tax >= 16777)
        {
            tax = Convert.ToDouble(label21.Text);
            value = tax - 16777 * 0.25 + 1250.00;
        }
        if (tax >= 33333)
        {
            tax = Convert.ToDouble(label21.Text);
            value = tax - 33333 * 0.30 + 5416.67;
        }
        if (tax >= 83333)
        {
            tax = Convert.ToDouble(label21.Text);
            value = tax - 33333 * 0.32 + 201416.67;
        }
        if (tax > 153846)
        {
            tax = Convert.ToDouble(label21.Text);
            value = tax - 153846 * 0.35 + 1001416.67;
        }
        label20.Text = value.ToString();
    }
public void wwtax()
{
双重税=0;
双值=0;
如果(税<10417)
{
tax=Convert.ToInt32(label21.Text);//10800值
label20.Text=“0”;
}
如果(税>10417)
{
tax=转换为32(label21.Text);
价值=税-10417*0.20;
}
如果(税>=16777)
{
tax=Convert.ToDouble(label21.Text);
价值=税收-16777*0.25+1250.00;
}
如果(税>=33333)
{
tax=Convert.ToDouble(label21.Text);
价值=税收-33333*0.30+5416.67;
}
如果(税>=83333)
{
tax=Convert.ToDouble(label21.Text);
价值=税-33333*0.32+201416.67;
}
如果(税收>153846)
{
tax=Convert.ToDouble(label21.Text);
价值=税收-153846*0.35+1001416.67;
}
label20.Text=value.ToString();
}

您使用的计算器可能只使用从左到右规则,其中表达式结果基于从左开始首先遇到的运算符

但在现实中,
10800-10417*0.20=8716.60
是正确的,
10800-10417*0.20=76.60
是错误的。因为除非明确指定,否则乘法是在减法之前完成的。原因很简单。BODMAS规则。只是或参考一些小学课本

如果你仍然需要你的计算器给出的答案,只需在表达式中需要先计算的部分使用括号即可。在您的情况下,
10800-10417


因此,您的表达式应该是:
value=(tax-10417)*0.20

似乎,您应该更改
if
的顺序(从
tax>153846开始),并使用
else if
。计算错误的直接原因是括号中的括号
(税-10417)*0.20米)


您表单中可能重复的响应是正确的-
10800-10417*0.20=76.6
是错误的。@xdtTransform 76.6与8716.6相比有点不准确,不是吗?:D使用小数不能解决这个问题…;-)你需要理解两者之间的差异(10800-10417)*0.20=76.60和10800-10417*0.20=8716.60一个好的计算器应该是8716.6。
    // When working with money, Decimal is a better choice than Double
    decimal value = 0;

    // Let's pull out tax from all the if's
    decimal tax = Convert.ToDecimal(label21.Text);

    if (tax > 153846)
      value = (tax - 153846) * 0.35m + 1001416.67m;
    else if (tax >= 83333)
      value = (tax - 33333) * 0.32m + 201416.67m;
    else if (tax >= 33333)
      value = (tax - 33333) * 0.30m + 5416.67m;
    else if (tax >= 16777)
      value = (tax - 16777) * 0.25m + 1250.00m;
    else if (tax > 10417)
      value = (tax - 10417) * 0.20m;
    else 
      value = 0;  

    // "f2": let's ensure 2 digits after the decimal point (i.e. "100.00" for 100)
    label20.Text = value.ToString("f2");