float --> int conversion

hi,
im sure this has been asked before but i cant find it anywhere- how do you convert a float to an int [and vice versa], in the game engine it can be done with non scripted properties but i cant figure how to do it when runninga script in normal blender.

thanks

will

int(3.14159)
float(3)

conversion to int truncates just like you’d expect in any other language [int(3.7) = 3] if you want to round you’ll need to round on your own [int(round(3.7, 0)) = 4]

thanks zero d :slight_smile:

You can round upward with: int (x + 0.5 ).

Note that, pragmatist that I am, I would nonetheless define a function called RoundUp() or somesuch, because I always want to document in my code whatever I am up to. Doing things this way makes the code self-explanatory.

why not use the built in round which works with negative numbers and is just as self-documenting?

if you want to be correct about it… the function already exists.

Use Ceil() to roud up and floor() to round down. Converting straight to an int works a lot of time, but if you’re looking for a specific type of result use those.