CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: 51/2 = 26?

  1. #1
    Join Date
    Sep 2005
    Posts
    13

    51/2 = 26?

    51/2=26?

    Why visual basic rounds up this calculation. I want it to round it down in order to get the answer 25.

    How must I do in order to make vb rounding the calculation down.

    I have made like this:

    dim result as integer

    x=51
    result=x/2
    Last edited by JackiePaper; October 25th, 2005 at 07:16 AM.

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: 51/2 = 26?

    Try this instead.
    Code:
     Debug.Print 51 \ 2
    This is known as integer division and will always return the lower value.

  3. #3
    Join Date
    Sep 2005
    Posts
    13

    Re: 51/2 = 26?

    OK. I solved that declaring result as double
    So it will calculate that 25,5. That's fine.

    But if I want to do 50/2 the result will be 25. How can I show the decimal. How I get the result 25,0.

  4. #4
    Join Date
    Jun 2002
    Location
    Clane, Ireland
    Posts
    766

    Re: 51/2 = 26?

    If I understand your question, you could use the Format command, when displaying the answer.

    eg txtBox = format(50/2,"#,##0.00")
    and txtbox = format(51/2,"#,##0.00")

    HTH
    JP

    Please remember to rate all postings.

  5. #5
    Join Date
    May 2001
    Posts
    91

    Re: 51/2 = 26?

    int or fix should do that. Depending on the question what you want to do if you're dividing something that return negative values:

    dim retval as integer

    retval = int(51/2) ' returns 25
    retval = fix(51/2) ' returns 25

    retval = int(-51/2) ' returns -26
    retval = fix(-51/2) ' returns -25
    have a nice day

    Patzer
    _____________________________
    Philo will never be forgotten

  6. #6
    Join Date
    Dec 2002
    Location
    London, UK
    Posts
    1,569

    Re: 51/2 = 26?

    Why don't you just use a Double data type?

    i.e.

    Dim retval As Double
    Mike

  7. #7
    Join Date
    Sep 2005
    Posts
    13

    Re: 51/2 = 26?

    I did as jp adviced and used format control. That's was the solution my problem. First I declared the result variable as double as Pinky said. Thanks to all helped me.
    Last edited by JackiePaper; October 26th, 2005 at 12:17 AM.

  8. #8
    Join Date
    Dec 2002
    Location
    London, UK
    Posts
    1,569

    Re: 51/2 = 26?

    glad to hear you came right.
    Mike

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

Featured