【问题标题】:Rounding a number to the nearest 5 or 10 or X将数字四舍五入到最接近的 5 或 10 或 X
【发布时间】:2010-09-24 11:57:15
【问题描述】:

给定 499、73433、2348 等数字,我可以使用什么 VBA 来四舍五入到最接近的 5 或 10?还是任意数字?

到 5 点:

 499 ->  500
2348 -> 2350
7343 -> 7345

10 点之前:

 499 ->  500
2348 -> 2350
7343 -> 7340

等等

【问题讨论】:

  • 谢谢大家的回答。我更聪明。抱歉,我只能将一个回复标记为正确,因为真正的“答案”分散在多条消息中。

标签: vba rounding


【解决方案1】:

这是简单的数学。给定一个数字 X 和一个舍入因子 N,公式为:

圆形(X / N)*N

【讨论】:

    【解决方案2】:

    综合答案

    X = 1234 'number to round
    N = 5    'rounding factor
    round(X/N)*N   'result is 1235
    

    对于浮点到整数,1234.564 到 1235,(这是 VB 特有的,大多数其他语言只是截断):

    int(1234.564)   'result is 1235
    

    注意: VB 使用Bankers Rounding,最接近的偶数,如果你不知道这可能会令人惊讶:

    msgbox round(1.5) 'result to 2
    msgbox round(2.5) 'yes, result to 2 too
    

    谢谢大家。

    【讨论】:

      【解决方案3】:

      四舍五入到最接近的 X(不特定于 VBA)

      N = X * int(N / X + 0.5)

      其中 int(...) 返回下一个最小的整数。

      如果您可用的舍入函数已经舍入到最近的整数,则省略 0.5

      【讨论】:

      • 澄清一下:int(N+0.5)round(N) 相同
      【解决方案4】:

      在 VB 中,math.round 有额外的参数来指定小数位数和舍入方法。 Math.Round(10.665, 2, MidpointRounding.AwayFromZero) 将返回 10.67 。如果数字是小数或单一数据类型,则 math.round 返回小数数据类型。如果是双精度,则返回双精度数据类型。如果选项严格打开,这可能很重要。

      (10.665).ToString("n2") 的结果从零四舍五入得到“10.67”。没有额外的参数 math.round 返回 10.66,这可能会导致不必要的差异。

      【讨论】:

        【解决方案5】:

        '示例:将 499 舍入到最接近的 5。您将使用 ROUND() 函数。

        a = inputbox("number to be rounded")
         b = inputbox("Round to nearest _______ ")
        
        
          strc = Round(A/B)
          strd = strc*B
        
        
         msgbox( a & ",  Rounded to the nearest " & b & ", is" & vbnewline & strd)
        

        【讨论】:

          【解决方案6】:

          对于严格的 Visual Basic 方法,您可以将浮点值转换为整数以四舍五入为所述整数。 VB 是一种罕见的类型转换语言(大多数其他语言只是截断。)

          5 或 x 的倍数可以简单地通过轮前除法和轮后乘法来完成。

          如果您想四舍五入并保留小数位,Math.round(n, d) 可以。

          【讨论】:

            【解决方案7】:

            这是我们的解决方案:

            Public Enum RoundingDirection
                Nearest
                Up
                Down
            End Enum
            
            Public Shared Function GetRoundedNumber(ByVal number As Decimal, ByVal multiplier As Decimal, ByVal direction As RoundingDirection) As Decimal
                Dim nearestValue As Decimal = (CInt(number / multiplier) * multiplier)
                Select Case direction
                    Case RoundingDirection.Nearest
                        Return nearestValue
                    Case RoundingDirection.Up
                        If nearestValue >= number Then
                            Return nearestValue
                        Else
                            Return nearestValue + multiplier
                        End If
                    Case RoundingDirection.Down
                        If nearestValue <= number Then
                            Return nearestValue
                        Else
                            Return nearestValue - multiplier
                        End If
                End Select
            End Function
            

            用法:

            dim decTotal as Decimal = GetRoundedNumber(CDec(499), CDec(0.05), RoundingDirection.Up)
            

            【讨论】:

            • 我认为这是 VB.NET 而不是 VBA?不过这个逻辑很有用!
            【解决方案8】:

            只需 ROUND(x/5)*5 即可。

            【讨论】:

              【解决方案9】:

              我无法添加评论,所以我将使用它

              在 vbs 中运行它并找出为什么 2 给出 2 的结果很有趣

              你不能相信轮回

               msgbox round(1.5) 'result to 2
               msgbox round(2.5) 'yes, result to 2 too
              

              【讨论】:

              • 我想说它要么与存储浮点数的方式有关,要么与国际标准化舍入算法的正确实现有关。我不知道它的名字,但是每隔一个 0.5 就向下取整,其余的向上取整。
              • 四舍五入 [原文如此] 到最接近的偶数。 SO中甚至有一个解释。
              • Vilx,你的建议很好,但不适用于 VBA round(465 / 10)*10 将返回 460
              • 好吧,这就是你的 FP 数学,克服它。大多数以 0.5 结尾的 FP 数字实际上是高于或低于所需值的一小部分,这会影响舍入。
              • @Manuel,我认为这被称为银行家四舍五入(最近的偶数),是许多变体之一。
              【解决方案10】:

              类似的东西?

              'nearest
               n = 5
               'n = 10
              
               'value
               v = 496
               'v = 499 
               'v = 2348 
               'v = 7343
              
               'mod
               m = (v \ n) * n
              
               'diff between mod and the val
               i = v-m
              
              
               if i >= (n/2) then     
                    msgbox m+n
               else
                    msgbox m
               end if
              

              【讨论】:

                【解决方案11】:

                试试这个功能

                -------------开始----

                Function Round_Up(ByVal d As Double) As Integer
                    Dim result As Integer
                    result = Math.Round(d)
                    If result >= d Then
                        Round_Up = result
                    Else
                        Round_Up = result + 1
                    End If
                End Function
                

                -------------结束------------

                【讨论】:

                  【解决方案12】:

                  我稍微更新了“社区维基”(最佳答案)提供的功能,只是四舍五入到最接近的 5(或任何你喜欢的),除了这个例外:四舍五入的数字永远不会优于原来的号码

                  这在需要说“一家公司存活了 47 年” 的情况下很有用:我希望网页显示 “存活超过 45 年”,同时避免谎称“活了50多年”

                  所以当你给这个函数输入 47 时,它不会返回 50,而是会返回 45。

                  'Rounds a number to the nearest unit, never exceeding the actual value
                  function RoundToNearestOrBelow(num, r)
                  
                      '@param         num         Long/Integer/Double     The number to be rounded
                      '@param         r           Long                    The rounding value
                      '@return        OUT         Long                    The rounded value
                  
                      'Example usage :
                      '   Round 47 to the nearest 5 : it will return 45
                      '   Response.Write RoundToNearestBelow(47, 5)
                  
                      Dim OUT : OUT = num
                  
                      Dim rounded : rounded = Round((((num)) / r), 0) * r
                  
                      if (rounded =< num) then
                          OUT = rounded
                      else
                          OUT = rounded - r
                      end if
                  
                      'Return
                      RoundToNearestOrBelow = OUT
                  
                  end function 'RoundToNearestOrBelow
                  

                  【讨论】:

                    【解决方案13】:

                    要在 Visual Basic 中模仿 Excel 中的 round 函数的工作方式,您只需使用: WorksheetFunction.Round(数字,小数)

                    这样银行或会计四舍五入就不会进行四舍五入。

                    【讨论】:

                    • 这不会按照问题中的要求将 2348 舍入到 2350(第二个示例)。
                    猜你喜欢
                    • 2011-09-21
                    • 1970-01-01
                    • 1970-01-01
                    • 2011-03-21
                    • 2012-03-07
                    • 1970-01-01
                    • 2017-07-26
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多