Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rounding to the nearest 5 or 10 1

Status
Not open for further replies.

ScubaStevo

Programmer
May 4, 2002
132
AU
How can I round a number to the nearest 5 or 10? So far I can only manage to the nearest whole number (or any number of of decimal places) with the Round function.
 
Try either:
Round(ANumber * 0.2) / 0.2
or:
Fix(Anumber * 0.2) / 0.2

depending on the type of rounding you need.

Hope this helps.
 
Round(yourNumber / 5) * 5

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Code:
Function basRound(Number As Double, Num_Digits As Integer)

    'If Num_Digits < 0, this "subtracts" the decade's values
    'If Num_Digits = 0, this returns the Integer Portion
    'If Num_Digits > 0, this returns the value "rounded" to the number of
    'Decimal palaces of Num_Digits.

    Dim dblNumNum_Digits As Double      'Set up variables
    Dim intNumNum_Digits As Long

    'Move decimal point such that int will perform at correct level
    dblNumNum_Digits = Number * (10 ^ Num_Digits)

    intNumNum_Digits = CLng(dblNumNum_Digits)       'Integerise

    'Compare integer to see if needs rounding up
    intNumNum_Digits = Round(dblNumNum_Digits, 0)   'Round

    'Return result, moving decimal point back
    basRound = intNumNum_Digits / (10 ^ Num_Digits)

End Function\

or

Code:
Public Function BasRound2Val(Number As Double, Multiple As Single) As Double

    'Michael Red.    2/16/2002.  To Round UP to the Multiple

    'Usage: ? BasRound2Val(1.001, 0.25)
            ' 1.25

    'Set up variables
    Dim dblDivided As Double
    Dim intDivided As Integer


    dblDivided = Number / Multiple      ''Divide

    intDivided = Int(dblDivided)        'Integerise

    intDivided = Round(dblDivided, 0)   'Round

    'Return result, returning to nearest multiple
    If (intDivided = dblDivided) Then
        BasRound2Val = (intDivided * Multiple)
     Else
        BasRound2Val = (intDivided * Multiple) + Multiple
    End If

End Function


MichaelRed


 
Very useful post!

I took the liberty of modifying BasRound2Val to get it working as an 'intelligent' maxima definer for graphs:
Code:
'** Modified 20050830 by PIB **
...
    If  Number > 15 And Multiple = 5 Then
        If Val(Right(Str(Int(Number)), 1)) <= 5 Then
            BasRound2Val = Number + (Multiple - (Number - (Int(Number / 10) * 10)))
        Else
            BasRound2Val = Number + (Multiple - (Number - (Int(Number / 10) * 10))) + 5
        End If
    Else
'** end of Modified 20050830 by PIB **

 'Return result, returning to nearest multiple
        If (intDivided = dblDivided) Then
            BasRound2Val = (intDivided * Multiple)
        Else
            BasRound2Val = (intDivided * Multiple) + Multiple
        End If
    End If
End Function

So it rounds up numbers>15 to the nearest 5 if the multiple passed is 5.

This may be just me being stupid, but I couldn't find a single command to get the decimal part of a number...



Phil

---------------
Pass me the ether.
 
Phil said:
" ... a single command ... "

hmmmmmmmmmmmmmmmmmmmmmmmm if you mean a compliment to CInt(X), then I also do nt know of " ... a single ... ". On the other hand,

FractX = X - CInt(X)

is a startign point for thoughts. I obviously doesn't actually work this easily, but it doesn't need that much elabration to get there.



MichaelRed


 
I think I was right on the me being stupid part ;)
Thanks.

Phil

---------------
Pass me the ether.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top