Results 1 to 6 of 6

Thread: [RESOLVED] Round to nearest specified value

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    816

    Resolved [RESOLVED] Round to nearest specified value

    I've been using this code to round off values but there is a problem when I try to round to a decimal value. RoundToNearestVal = 0.1 and a Double or Variant type. It takes the value as 0 so I get a division by 0 error. What am I doing wrong here?

    Code:
    Public Function RoundToNearest(NumberToRound, RoundToNearestVal)
    'Function rounds up or down to the nearest value
        RoundToNearest = RoundToNearestVal * (NumberToRound \ RoundToNearestVal)
        If Abs(NumberToRound - RoundToNearest) > RoundToNearestVal / 2 Then
            If NumberToRound >= 0 Then
                RoundToNearest = RoundToNearest + RoundToNearestVal
            Else
                RoundToNearest = RoundToNearest - RoundToNearestVal
            End If
        End If
    End Function
    Attached Images Attached Images  

  2. #2
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Round to nearest specified value

    \ is an operator for Long or Integer data type.
    x = 1000 \ 0.1 = CInt(1000) \ CInt(0.1) = 1000 \ 0 : Division by zero!

    With Variant/Double, change to:
    Code:
    RoundToNearest = RoundToNearestVal * Int(NumberToRound / RoundToNearestVal)
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    816

    Re: Round to nearest specified value

    Hi anhn. Not sure I understand. NumberToRound \ RoundToNearestVal still returns <Division by zero> and causes an error.
    Why is NumberToRound \ RoundToNearestVal set to a Variant/Integer type in the watch window?

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Round to nearest specified value

    Just as anhn said, these are implicitly converted to integers or long before division, when you use the \ operator.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    816

    Re: Round to nearest specified value

    Ohh.. I didn't notice the \ / difference.

    Thankyou both!

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Round to nearest specified value

    \ = integer division
    / = decimal division
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

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