Rounding a number to the nearest 5 or 10 or X

前端 未结 13 1649
臣服心动
臣服心动 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:17

    In VB, math.round has additional arguments to specify number of decimal places and rounding method. Math.Round(10.665, 2, MidpointRounding.AwayFromZero) will return 10.67 . If the number is a decimal or single data type, math.round returns a decimal data type. If it is double, it returns double data type. That might be important if option strict is on.

    The result of (10.665).ToString("n2") rounds away from zero to give "10.67". without additional arguments math.round returns 10.66, which could lead to unwanted discrepancies.

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

    Try this function

    --------------start----------------

    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
    

    -------------end ------------

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

    I slightly updated the function provided by the "community wiki" (the best answer), just to round to the nearest 5 (or anything you like), with this exception : the rounded number will NEVER be superior to the original number.

    This is useful in cases when it is needed to say that "a company is alive for 47 years" : I want the web page to display "is alive for more than 45 years", while avoiding lying in stating "is alive for more than 50 years".

    So when you feed this function with 47, it will not return 50, but will return 45 instead.

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

    For a strict Visual Basic approach, you can convert the floating-point value to an integer to round to said integer. VB is one of the rare languages that rounds on type conversion (most others simply truncate.)

    Multiples of 5 or x can be done simply by dividing before and multiplying after the round.

    If you want to round and keep decimal places, Math.round(n, d) would work.

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

    I cannot add comment so I will use this

    in a vbs run that and have fun figuring out why the 2 give a result of 2

    you can't trust round

     msgbox round(1.5) 'result to 2
     msgbox round(2.5) 'yes, result to 2 too
    
    0 讨论(0)
  • 2020-11-28 08:24

    Simply ROUND(x/5)*5 should do the job.

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