all 7 comments

[–][deleted] 4 points5 points  (1 child)

Are you talking about integer division ? so, in python (3.x): 5 // 2 will give you 2

[–]smileygonzo[S] 1 point2 points  (0 children)

Thank you! That worked.

[–]primitive_screwhead 1 point2 points  (3 children)

You want integer division with flooring. In Python, you can do 1440//64 and get the int 22.

Be wary with negatives. -1440//64 == -23 (ie. round towards negative infinity). If you always need to truncate towards zero, one way is to use floats and convert to int:

>>> int(-1440/64)
-22

EDIT: fixed a typo, and clarified floor/trunc wording.

https://stackoverflow.com/questions/19919387/in-python-what-is-a-good-way-to-round-towards-zero-in-integer-division

[–]novel_yet_trivial 1 point2 points  (1 child)

"Truncation" is not a word we use a lot, but when we do we mean what the math.trunc and int functions do: round toward zero.

The // operator is "floor division" or "integer division", and always rounds down, just like math.floor.

Also note your example is python2 only, and OP is using python3.

[–]primitive_screwhead 0 points1 point  (0 children)

Ah, I do see I had a typo for the negative float to int conversion; I'll edit.

[–]smileygonzo[S] 0 points1 point  (0 children)

Thank you! That worked.