【问题标题】:How to Round to the nearest whole number in C#如何在C#中四舍五入到最接近的整数
【发布时间】:2012-01-13 01:11:25
【问题描述】:

如何将值四舍五入到最接近的整数?

例如:

1.1 => 1
1.5 => 2
1.9 => 2

“Math.Ceiling()”对我没有帮助。有什么想法吗?

【问题讨论】:

标签: c# rounding


【解决方案1】:

请参阅official documentation 了解更多信息。例如:

基本上你给Math.Round方法三个参数。

  1. 要四舍五入的值。
  2. 值后要保留的小数位数。
  3. 您可以调用一个可选参数来使用 AwayFromZero 舍入。 (忽略,除​​非舍入不明确,例如 1.5

示例代码:

var roundedA = Math.Round(1.1, 0); // Output: 1
var roundedB = Math.Round(1.5, 0, MidpointRounding.AwayFromZero); // Output: 2
var roundedC = Math.Round(1.9, 0); // Output: 2
var roundedD = Math.Round(2.5, 0); // Output: 2
var roundedE = Math.Round(2.5, 0, MidpointRounding.AwayFromZero); // Output: 3
var roundedF = Math.Round(3.49, 0, MidpointRounding.AwayFromZero); // Output: 3

Live Demo

如果您希望将 0.5 值向上取整,则需要 MidpointRounding.AwayFromZero。不幸的是,这不是Math.Round() 的默认行为。如果使用MidpointRounding.ToEven(默认值),则该值将四舍五入为最接近的偶数数(1.5 四舍五入为2,但2.5 也四舍五入为2)。

【讨论】:

  • 另一方面,使用away from zero也意味着-1.5将四舍五入为-2
  • 使用 Math.Ceiling,使用 Math.Round 摩擦不是一个好习惯,阅读:stackoverflow.com/questions/9221205/…
  • 我发现 Math.Round(1.5, 0) 返回 2
  • @davogotland 是他们无论如何要从 137.5 到 140 而不是 138 ?我的意思是四舍五入到最接近的十分之一?
  • @sam 可能除以 10,然后用 Math.Ceiling 四舍五入,最后乘以 10?
【解决方案2】:
Math.Ceiling

总是向上取整(朝向天花板)

Math.Floor

总是向下舍入(朝向地板)

你所追求的只是

Math.Round

根据this post进行舍入

【讨论】:

  • 他们无论如何都要将 137.5 舍入到 140 而不是 138 吗?我的意思是四舍五入到最接近的十分之一?
【解决方案3】:

您需要Math.Round,而不是Math.CeilingCeiling 总是向上“四舍五入”,而 Round 根据小数点后的值向上或向下舍入。

【讨论】:

    【解决方案4】:

    有这本手册,也有点可爱:

    double d1 = 1.1;
    double d2 = 1.5;
    double d3 = 1.9;
    
    int i1 = (int)(d1 + 0.5);
    int i2 = (int)(d2 + 0.5);
    int i3 = (int)(d3 + 0.5);
    

    只需将 0.5 加到任何数字上,然后将其转换为 int(或将其取整),它就会在数学上正确四舍五入:D

    【讨论】:

    • 它看起来仍然很可疑。首先,问题是关于四舍五入up,其次,我刚才尝试的时候,Math.Round(1.5)的默认实现四舍五入为2。所以这可能不是他想要的。跨度>
    • 另外,您的示例将小数点与小数点逗号混合。您通常使用哪一种(我猜是在瑞典)? :)
    • 哎呀...哦,是的,对不起。当然在编程小数点时,但在正式文本中我们使用小数点逗号。是的,瑞典^^关于这个问题,以及“四舍五入”部分:我认为这只是一些语言错误。在 op 给出的示例中,一些十进制数向下舍入。
    • @ver 我不使用 Math.Round 进行舍入,而是使用演员表进行舍入。这就是为什么这种方式是手动的而且有点可爱;)
    【解决方案5】:

    您可以按照其他人的建议(推荐)使用 Math.Round,或者您可以添加 0.5 并强制转换为 int(这将删除小数部分)。

    double value = 1.1;
    int roundedValue = (int)(value + 0.5); // equals 1
    
    double value2 = 1.5;
    int roundedValue2 = (int)(value2 + 0.5); // equals 2
    

    【讨论】:

      【解决方案6】:

      只是一个提醒。小心双标。

      Math.Round(0.3 / 0.2 ) result in 1, because in double 0.3 / 0.2 = 1.49999999
      Math.Round( 1.5 ) = 2
      

      【讨论】:

        【解决方案7】:

        你有 Math.Round 函数,它完全符合你的要求。

        Math.Round(1.1) results with 1
        Math.Round(1.8) will result with 2.... and so one.
        

        【讨论】:

        • 1.5 作为值呢?您需要更多参数。
        【解决方案8】:

        这将四舍五入到最接近的 5 或者如果它已经被 5 整除则不改变

        public static double R(double x)
        {
            // markup to nearest 5
            return (((int)(x / 5)) * 5) + ((x % 5) > 0 ? 5 : 0);
        }
        

        【讨论】:

          【解决方案9】:

          我一直在寻找这个,但我的示例是取一个数字,例如 4.2769,然后将其放在一个跨度中,即 4.3。不完全相同,但如果这有帮助:

          Model.Statistics.AverageReview   <= it's just a double from the model
          

          然后:

          @Model.Statistics.AverageReview.ToString("n1")   <=gives me 4.3
          @Model.Statistics.AverageReview.ToString("n2")   <=gives me 4.28
          

          等等……

          【讨论】:

          • 我正在使用你的方法,因为我还需要一个字符串,并且 .ToString("n0") 会为我处理舍入:1.5m.ToString("n0") // 返回 "2 "
          【解决方案10】:

          使用Math.Round:

          double roundedValue = Math.Round(value, 0)
          

          【讨论】:

            【解决方案11】:
            var roundedVal = Math.Round(2.5, 0);
            

            它会给出结果:

            var roundedVal = 3
            

            【讨论】:

              【解决方案12】:

              使用Math.Round(number) 四舍五入到最接近的整数。

              【讨论】:

                【解决方案13】:

                如果您使用整数而不是浮点数,这就是方法。

                #define ROUNDED_FRACTION(numr,denr) ((numr/denr)+(((numr%denr)<(denr/2))?0:1))
                

                这里 "numr""denr" 都是无符号整数。

                【讨论】:

                  【解决方案14】:

                  编写你自己的回合方法。类似的,

                  function round(x) rx = Math.ceil(x) if (rx - x <= .000001) return int(rx) else return int(x) end

                  【讨论】:

                    【解决方案15】:
                    decimal RoundTotal = Total - (int)Total;
                    if ((double)RoundTotal <= .50)
                       Total = (int)Total;
                    else
                       Total = (int)Total + 1;
                    lblTotal.Text = Total.ToString();
                    

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 2012-09-03
                      • 1970-01-01
                      • 2011-03-21
                      • 1970-01-01
                      • 2017-10-05
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多