Rounding a number to the nearest 5 or 10 or X

前端 未结 13 1651
臣服心动
臣服心动 2020-11-28 08:11

Given numbers like 499, 73433, 2348 what VBA can I use to round to the nearest 5 or 10? or an arbitrary number?

By 5:

 499 ->  500
2348 -> 2350         


        
相关标签:
13条回答
  • 2020-11-28 08:27

    something like that?

    '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 讨论(0)
  • 2020-11-28 08:31

    To round to the nearest X (without being VBA specific)

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

    Where int(...) returns the next lowest whole number.

    If your available rounding function already rounds to the nearest whole number then omit the addition of 0.5

    0 讨论(0)
  • 2020-11-28 08:31

    Here is our solution:

    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
    

    Usage:

    dim decTotal as Decimal = GetRoundedNumber(CDec(499), CDec(0.05), RoundingDirection.Up)
    
    0 讨论(0)
  • 2020-11-28 08:33

    It's simple math. Given a number X and a rounding factor N, the formula would be:

    round(X / N)*N

    0 讨论(0)
  • 2020-11-28 08:35

    'Example: Round 499 to nearest 5. You would use the ROUND() FUNCTION.

    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)
    
    0 讨论(0)
  • 2020-11-28 08:42

    Integrated Answer

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

    For floating point to integer, 1234.564 to 1235, (this is VB specific, most other languages simply truncate) do:

    int(1234.564)   'result is 1235
    

    Beware: VB uses Bankers Rounding, to the nearest even number, which can be surprising if you're not aware of it:

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

    Thank you everyone.

    0 讨论(0)
提交回复
热议问题