Results 1 to 6 of 6

Thread: How to make a value to nearest ten (e.g. 47 to 50 or 43 to 40)

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2007
    Posts
    6

    How to make a value to nearest ten (e.g. 47 to 50 or 43 to 40)

    How to make a value to nearest ten (e.g. 47 to 50 or 43 to 40)

    Please help me for VB6.

  2. #2
    Addicted Member TBeck's Avatar
    Join Date
    Apr 2006
    Location
    Ontario, Canada
    Posts
    254

    Re: How to make a value to nearest ten (e.g. 47 to 50 or 43 to 40)

    something like this:

    vbcode Code:
    1. 'uses the modulus operataor to give the remainder when toBeRounded is divided by 10
    2. lastDigit = toBeRounded Mod 10
    3. 'assues 5 means round up and 4 is round down
    4. If lastDigit >= 5 Then
    5.     roundedValue = toBeRounded - lastDigit + 10
    6. Else
    7.     roundedValue = toBeRounded - lastDigit
    8. End If

  3. #3
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: How to make a value to nearest ten (e.g. 47 to 50 or 43 to 40)

    Code:
    MsgBox Round(47 / 10) * 10
    MsgBox Round(43 / 10) * 10

  4. #4
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: How to make a value to nearest ten (e.g. 47 to 50 or 43 to 40)

    another way (with 5 rounding up):
    Code:
    Dim N As Long 
    N = 45
    
    Debug.Print (N \ 10 + (N Mod 10) \ 5) * 10

  5. #5
    Addicted Member TBeck's Avatar
    Join Date
    Apr 2006
    Location
    Ontario, Canada
    Posts
    254

    Re: How to make a value to nearest ten (e.g. 47 to 50 or 43 to 40)

    Oh yea, i forgot about the round command... i should really install my vb6 instead of still using vb5

  6. #6
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: How to make a value to nearest ten (e.g. 47 to 50 or 43 to 40)

    NearestTen = CLng(Value / 10) * 10

    Note that this will take 45 down to 40. If you want the midpoint to always go up, then use this:

    NearestTen = 10 * Int((Value + 5) / 10)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width