Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a number into the integer and decimal parts

Tags:

python

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?

like image 589
Double AA Avatar asked Jul 13 '11 15:07

Double AA


Video Answer


2 Answers

Use math.modf:

import math x = 1234.5678 math.modf(x) # (0.5678000000000338, 1234.0) 
like image 153
mhyfritz Avatar answered Oct 08 '22 15:10

mhyfritz


We can use a not famous built-in function; divmod:

>>> s = 1234.5678 >>> i, d = divmod(s, 1) >>> i 1234.0 >>> d 0.5678000000000338 
like image 24
utdemir Avatar answered Oct 08 '22 13:10

utdemir