python - remove all decimals from a float

What about converting it to int?

>>>int(a)
100

Just for the sake of completeness, there are many many ways to remove the decimal part from a string representation of a decimal number, one that I can come up right now is:

s='100.0'
s=s[:s.index('.')]
s
>>>'100'

Perhaps there's another one more simple.

Hope this helps!


If you do not want to convert it to an int you can also split it.

>>> a = 100.25
>>> str(a).split('.')[0]
>>> '100'  # result is now a string

If you're deriving the float you can floor it with //

a = 200 / 2 # outputs 100.0
a = 200 // 2 # outputs 100

Faster than typecasting afterwards!

Tags:

Python