Results 1 to 12 of 12

Thread: Round

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 1999
    Location
    Ruinen, Drente, Netherlands
    Posts
    192
    How do I make Rounding, for example:
    0.125 must be: 1
    1.75 must be: 2
    2.05 must be: 3
    5.118 must be: 6

  2. #2
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    Use Int or, if you have vb6 Fix.
    Code:
    Number = Int(Number + 1)
    To get information on using Fix, look up msdn.microsoft.com

  3. #3
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Look what I've found:
    Code:
    Dim nr As Integer
    nr = 1.9
    MsgBox Int(nr + 1) 'Returns 3!
    So instead use
    Code:
    Dim nr As Integer
    nr = 1.99
    Number = Int(nr + 1#)
    '1# stands for 0.9999999999999999 (0.sixteen 9's)
    'You have to use that 1# because otherwise you'll get:
    nr = 1.00000000000001
    MsgBox Int(nr + 0.999999999999999) '(0.fifteen 9's)
    'Returns 1, not exactely what you want huh?
    VB automaticly replaces the 0.sixteen 9's to 1#, thats how I discovered that cool huh?

    have fun man!


    [Edited by Jop on 10-02-2000 at 01:26 PM]
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Jop: In your first post you seemed surprised that Int(nr + 1) resulted in a value of 3. The reason that happens is that you defined nr as an Integer. When you attempted to store a non-integer value in it by writing nr = 1.9, VB made it an integer by rounding so it became = 2. The following illustrates that happening.

    Dim nr As Integer
    nr = 1.9
    MsgBox nr
    MsgBox Int(nr + 1) 'Returns 3!

  5. #5
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Yeah I realised that, I was retesting it and it didn't seem to work

    but now I have this strange thing:
    Code:
    Dim nr As Long 'LONG!
    nr = 1.9
    MsgBox nr '2
    MsgBox Int(nr + 1#) '3
    how do I make it so it doesn't round it itself before processing?
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  6. #6

  7. #7
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Thanx, didn't knew about the Long Integer

    I learn new things every day!
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  8. #8
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    Originally posted by MartinLiss
    Use Double or Currency.
    Or Single.

  9. #9
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    What's the difference between double & single?
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  10. #10
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    I think it something like this: (look at msdn if you want to be sure)
    Double:14 decimals
    Single:7 decimals

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Apr 1999
    Location
    Ruinen, Drente, Netherlands
    Posts
    192
    Thanks to you all ! I think I found a solution:
    INT and FIX rounds downwards, so I make it this way:
    Number! = Number! + 0.99
    and it works !

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Apr 1999
    Location
    Ruinen, Drente, Netherlands
    Posts
    192
    Sorry , it must be:
    Number! = FIX(number + 0.99)

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