Floating Point to Integer?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PythonNotSoGuru
    New Member
    • Oct 2007
    • 21

    Floating Point to Integer?

    Hi all first time posting in a while sry if i mess something up. Anyway I am using 2d arrays to make a game with pygame and i alter coordinates on this array by smaller than 1 increments. Then i need to reference the coordinates in an If statement. However by this time in the program the coordinates have been altered to non integer numbers. My question is how can i turn these floating point numbers into whole integers.

    code = [python]

    PS: I have already tried the round function. This does give integers but it adds a .0 to the end so like the floating point number 89.9 turns into 90.0 and i cant use it seeing as how it has a decimal place after the integer.

    thanx in advance
    alex
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    Use the built in function int(). int(x) returns an integer version of x. This works on more than just floats; you can use it on strings or any object type that supports it. There is also a function float(), a function str(), a function list(), a function tuple(), a function bool(), and others.
    I hope this helps.
    Edit:
    One thing to note; the value int(x) returns will have the digits after the decimal point truncated, not rounded, so int(1.75) will return 1. You may want to use int(round(x)).

    Comment

    • PythonNotSoGuru
      New Member
      • Oct 2007
      • 21

      #3
      great thanx

      cool thanx alot man works nicely

      Comment

      Working...