简体   繁体   English

除两位小数时在 C# 中得到不正确的输出

[英]Getting incorrect output in C# when dividing two decimals

I have a program which divides a variable with another variable.我有一个程序将一个变量除以另一个变量。 But the answer im getting is incorrect:!!但是我得到的答案是不正确的:!! Please check image below :请检查下图:

升

Code Below:代码如下:

      iter_defects++;
      df1[iter_defects].blob_id = b.Id;
      decimal convertX = Convert.ToDecimal(txtmmtopixX.Text);
      decimal convertY = Convert.ToDecimal(txtmmtopixY.Text);
      df1[iter_defects].count = iter_defects;

      df1[iter_defects].cog_X = Math.Round( Convert.ToDecimal( b.Centroid.X)) / convertX;
      df1[iter_defects].cog_Y = Math.Round(Convert.ToDecimal(b.Centroid.Y ))/ convertY;
     if (df1[iter_defects].cog_X > 10 && df1[iter_defects].cog_X < 30)
      {

     if (df1[iter_defects].BY > 1 && df1[iter_defects].BY < 74)
      {
       df1[iter_defects].cog_X = df1[iter_defects].cog_X - 20;

       }

      if (df1[iter_defects].BY > 64 && df1[iter_defects].BY < 129)
        {
         df1[iter_defects].cog_X = df1[iter_defects].cog_X - 20;

         }

      if (df1[iter_defects].BY > 118 && df1[iter_defects].BY < 183)
         {
           df1[iter_defects].cog_X = df1[iter_defects].cog_X - 20;

         }

       }

b.centroid.X and b.centroid.Y are double by default, so im converting it to decimal. b.centroid.X 和 b.centroid.Y 默认是双倍的,所以我把它转换成十进制。

If I divide 147 by 9.3 i should get approx 16, but im getting 7.9!如果我将 147 除以 9.3,我应该得到大约 16,但我得到 7.9!

Earlier I was using double instead of decimal, but even decimal is giving same issues.早些时候我使用的是双精度而不是十进制,但即使是十进制也会出现同样的问题。 Have pinned the variable values on the image on the right.已将变量值固定在右侧的图像上。

What could be wrong?有什么问题吗?

UPDATE:更新:

In my nested if statement I so some more arithmetic operation based on few conditions.在我的嵌套 if 语句中,我根据几个条件进行了更多算术运算。 When I removed those conditions the calculation is happening just fine.当我删除这些条件时,计算进行得很好。 More confused now.现在更糊涂了。 The code below works, but without those conditions, my code after this section wont work:下面的代码可以工作,但如果没有这些条件,我在本节之后的代码将无法工作:

  iter_defects++;
  df1[iter_defects].blob_id = b.Id;
  decimal convertX = Convert.ToDecimal(txtmmtopixX.Text);
  decimal convertY = Convert.ToDecimal(txtmmtopixY.Text);
  df1[iter_defects].count = iter_defects;

  df1[iter_defects].cog_X = Math.Round( Convert.ToDecimal( b.Centroid.X)) / convertX;
  df1[iter_defects].cog_Y = Math.Round(Convert.ToDecimal(b.Centroid.Y ))/ convertY;
 if (df1[iter_defects].cog_X > 10 && df1[iter_defects].cog_X < 30)
  {
  df1[iter_defects].cog_X = df1[iter_defects].cog_X - 20;
  }

It's not entirely clear how your program works, but I assume the problem is probably because you are in a loop of some kind and by the time you are checking your results, more than one iteration has occurred and the base values (ex cog_X ) have changed, so your comparison math won't match.目前还不完全清楚你的程序是如何工作的,但我认为问题可能是因为你处于某种循环中,当你检查结果时,已经发生了不止一次迭代并且基值(ex cog_X )有改变了,所以你的比较数学将不匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM