0

我的程序无法确定是Math.Round作为 adecimal还是 a执行double,但我不知道如何解决这个问题......这是我的代码,虽然倒数第二行是我所关心的。

 ArrayList topp1 = new ArrayList();
 int toppcount = 0;
 foreach (Control cb in GroupBoxH1T.Controls)
 {
     CheckBox cb1 = cb as CheckBox;
     if (cb1.Checked == true)
     {
          toppcount++;
          topp1.Add(cb1.Text);
     }
  }

  if (cbhwchoice.Checked == false)
  {
      ArrayList topp2 = new ArrayList();
      foreach (Control cb in GroupBoxH2T.Controls)
      {
          CheckBox cb1 = cb as CheckBox;
          if (cb1.Checked == true)
          {
              toppcount++;
              topp2.Add(cb1.Text);
          }
      }

      toppcount = Math.Round((toppcount/2,MidpointRounding.AwayFromZero);
  }
4

3 回答 3

7

Math.Round需要一个浮点数或十进制数,因为在整数上调用它不会有任何效果。如果要调用它,请传入该类型的值。为此,您可以简单地将分子和分母转换为所需的类型。例如:

decimal value = Convert.ToDecimal(toppcount) / 2.0M;
toppcount = Math.Round(value, MidpointRounding.AwayFromZero);
于 2012-05-13T02:30:28.217 回答
1

在倒数第二行

 toppcount = Math.Round((toppcount/2,MidpointRounding.AwayFromZero);

toppcount也是integer
2integer
所以toppcount/2会给你integer
作为例子 1/2会给你0

尝试Convert.ToDecimal(toppcount)/2.0(Decimal)toppcount/2.0

于 2012-05-13T02:30:52.417 回答
0

在 if 块的最后一条语句中将整数 2 替换为十进制 2.0。所以语句会变成这样:

toppcount = Math.Round((toppcount/2.0))
于 2017-08-25T12:35:15.640 回答