splitting a number into the integer and decimal parts

Double AA picture Double AA · Jul 13, 2011 · Viewed 129.8k times · Source

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?

Answer

mhyfritz picture mhyfritz · Jul 13, 2011

Use math.modf:

import math
x = 1234.5678
math.modf(x) # (0.5678000000000338, 1234.0)