Is there a pythonic way of splitting a number such as 1234.5678
into two parts (1234, 0.5678)
i.e. the integer part and the decimal part?
Use math.modf
:
import math x = 1234.5678 math.modf(x) # (0.5678000000000338, 1234.0)
We can use a not famous built-in function; divmod:
>>> s = 1234.5678 >>> i, d = divmod(s, 1) >>> i 1234.0 >>> d 0.5678000000000338
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With