Results 1 to 7 of 7

Thread: Python Rounding

  1. #1
    Join Date
    Jan 2011
    Beans
    10

    Python Rounding

    So, i need to round a number, but the number can't be a decimal number.
    So it can't be fx 40.0, it must be 40
    And it's rounding to a precision of 1, so 40.1 would be rounded to 40.
    Thanks ahead.

  2. #2
    Join Date
    Jun 2009
    Beans
    352

    Re: Python Rounding

    I would use the round function and then cast the result as an int.

  3. #3
    Join Date
    Jan 2011
    Beans
    10

    Re: Python Rounding

    I tried, but it didn't work in this case of some reason. Well, it worked, but not fully.
    Trying to find out if the problem is something else, which is what I really DON'T hope.

  4. #4
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,165
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Python Rounding

    Quote Originally Posted by noodleman999 View Post
    Well, it worked, but not fully.
    Cana you elaborate on that (input and expected/effective results)? And what is your definition of "round"?

  5. #5
    Join Date
    Jan 2011
    Beans
    10

    Re: Python Rounding

    Okay, i'm currently making a PyGame program, and i want the background to slowly change from black to green, and back.
    Therefore, i can't use +1, as it goes too quick
    I need to use 0.1
    But the pygame.Color function can't take decimals.
    After that, i tried:
    Code:
    rgc = round(rgc)
    rgc = int(rgc)
    And it went to green, fine. But as soon as it tried backwards: Error saying invalid color arguement.

  6. #6
    Join Date
    Jun 2009
    Beans
    352

    Re: Python Rounding

    Without seeing the code, I can't tell what in particular is wrong with, but your approach seems to use floats needlessly. It seems better to use integers throughout, for example only incrementing the color value every 10 passes of the loop.

  7. #7
    Join Date
    Jan 2011
    Beans
    10

    Re: Python Rounding

    Quote Originally Posted by JDShu View Post
    Without seeing the code, I can't tell what in particular is wrong with, but your approach seems to use floats needlessly. It seems better to use integers throughout, for example only incrementing the color value every 10 passes of the loop.
    I'll try. Thanks for the tip

Bookmarks

Posting Permissions

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