将数字四舍五入到最接近的5或10或X.

问题描述 投票:57回答:13

鉴于像499,73433,2348这样的数字,我可以使用什么VBA来舍入到最接近的5或10?或任意数字?

5点:

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

到10:

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

等等

vba rounding
13个回答
89
投票

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

圆形(X / N)* N.


0
投票

这样的事情?

'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

0
投票

试试这个功能

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

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

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


0
投票

我稍微更新了“社区维基”提供的功能(最佳答案),只是四舍五入到最接近的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

-1
投票

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

这种方式银行或会计四舍五入不进行四舍五入。


32
投票

综合答案

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

谢谢大家。


11
投票

舍入到最近的X(没有特定于VBA)

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

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

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


9
投票

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

(10.665).ToString(“n2”)的结果从零开始舍入到“10.67”。如果没有其他参数,math.round将返回10.66,这可能会导致不必要的差异。


3
投票

'示例:将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)

1
投票

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

5或x的倍数可以简单地通过在该轮之前分割并在该轮之后相乘来完成。

如果要舍入并保持小数位,Math.round(n,d)将起作用。


1
投票

这是我们的解决方案:

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)

0
投票

Simply ROUND(x / 5)* 5应该可以胜任。


0
投票

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

在一个vbs运行中,并有乐趣搞清楚为什么2给出2的结果

你不能相信圆

 msgbox round(1.5) 'result to 2
 msgbox round(2.5) 'yes, result to 2 too
© www.soinside.com 2019 - 2024. All rights reserved.