I need to split a float in 2, but the split function doesnt work with floats...
What is an alternative for this?

If I have 2.4 I want to get 2 and 4
In math you've got a function fpart and ipart, but I don't know this is possible in python...

Recommended Answers

All 4 Replies

Let's assume you want something like this ...

x = 123.456

ipart, fpart = str(x).split('.')

print ipart, fpart

Okay that was quit stupid :f

Okay that was quit stupid :f

Asking questions is never stupid! :)

Alternately you could use modulus division like this:

>>> f = 123.456
>>> ipart = int(f)
>>> fpart = f % 1
>>> ipart
123
>>> fpart
0.45600000000000307
>>>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.