Results 1 to 15 of 15

Thread: Always Round up

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    hi, how do I always round a number up a number to the whole digit?

    thanks

    dimava
    NXSupport - Your one-stop source for computer help

  2. #2
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151

    Add .999?

    This should almost always work.

    Add .99999999999 to the number and then convert to integer. I assume you can figure out the code to do it.
    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

  3. #3
    Addicted Member
    Join Date
    Mar 2001
    Posts
    157
    You need to add.4999999999 instead of .9999999999.
    CInt(4.9 + .999999) returns 6.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    ok, thanks guys
    NXSupport - Your one-stop source for computer help

  5. #5
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151

    Strange roundoff error.

    Strange that 4.9 + .9999999 equals 6. Are you sure? Wouldn't you get 5.9 if you added one? How could adding .999999 result in more than adding one?

    I thought you wanted the next higher integer no matter what. If so, perhaps truncate to integer and then add one.

    Adding .499999 does not seem correct. Why not .5?
    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Cint doesnt truncate to integer, it rounds to closest integer
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    In that case it depends on which function you use. If I remember right, Int() just truncates the number.
    Harry.

    "From one thing, know ten thousand things."

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    yeah but i think you should round with cint this time:
    Cint(number+0.5)
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  9. #9
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    You guys have forgotten that if int(number) = number then you shouldn't add anything to the number, however, if int(number) <> number then you should add an integer. That's how to accurately round up.

    Don't forget to allow for negative numbers though.

  10. #10
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Well not really, if you notice Guv suggested adding 0.999999 or thereabouts. If you add this to an integer and truncate it, you get the original integer.
    Harry.

    "From one thing, know ten thousand things."

  11. #11
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Harry, what about if I have 1.0000001?

    1.00000001
    0.99999990+
    ---------------
    1.99999991 which will be rounded down to 1.

    On the other hand, (int(1.0000001) = 1.0000001) will return false so you can catch the error and round it to 2 rather than 1.

    Comparing int(number) vs number is the most accurate way I can think of to do this...

  12. #12
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Well there is a limit to how accurately a floating point number can be represented in binary, so it is certainly possible to add the closest value to 1 that is less than 1. At greater accuracy than that, it's pointless trying to be accurate because you are limited by the machine.
    Harry.

    "From one thing, know ten thousand things."

  13. #13
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Sometimes it seems like the last digit is dropped off when displayed, but i guess the displayed value is not equal to the stored, if you pass 0.999999999 there might just be a fraction that is smaller than 0.000000001 thats why i suggested cint(x+0.5)
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  14. #14
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Kedaman, I don't think your appraoch is necessarily appropriate. Consider if I want to round the two numbers 0 and 1 to nearest integer that is they should return 0 and 1:

    debug.print CInt(0 + 0.5)
    debug.print CInt(1 + 0.5)

    This is because CInt and CLng always round it to the nearest even number.

    Better is to compare int(number) vs number.

  15. #15
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    youre right.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

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