tags:

views:

70

answers:

3
a=123.45324

is there a function that will return just 123?

+2  A: 

Python 2.x:

import math
int( math.floor( a ) )

N.B. Due to complicated reasons involving the handling of floats, the int cast is safe.

Python 3.x:

import math
math.floor( a )
katrielalex
what is NB, btw i sent u an email a few days ago, did u get it?
I__
what do you think of mark's method?
I__
hehe yep, will reply when I have free time =p. I've commented on Mark's method.
katrielalex
http://en.wikipedia.org/wiki/Nota_bene
katrielalex
gotcha, i thought u were saying something in hebrew
I__
העברית נראה אחרת =p
katrielalex
+4  A: 

int will always truncate towards zero:

>>> a = 123.456
>>> int(a)
123
>>> a = 0.9999
>>> int(a)
0
>>> int(-1.5)
-1

The difference between int and math.floor is that math.floor returns the number as a float, and does not truncate towards zero.

Mark Rushakoff
`math.floor` does what it should (truncate towards negative infinity). In Py3k it returns an `int` (thankfully =p)
katrielalex
+2  A: 
a = 123.45324
int(a)
Artur Gaspar