Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in python, how do i split a number by the decimal point

So if I run:

a = b / c

and get the result 1.2234

How do i separate it so that I have:

a = 1
b = 0.2234
like image 595
joe schmoe Avatar asked Aug 10 '10 22:08

joe schmoe


People also ask

How do you divide two decimal places in Python?

Just use the formatting with %. 2f which gives you round down to 2 decimal points.

How do you get two values after a decimal in Python?

In Python, to print 2 decimal places we will use str. format() with “{:. 2f}” as string and float as a number. Call print and it will print the float with 2 decimal places.


1 Answers

>>> from math import modf
>>> b,a = modf(1.2234)
>>> print ('a = %f and b = %f'%(a,b))
a = 1.000000 and b = 0.223400
>>> b,a = modf(-1.2234)
>>> print ('a = %f and b = %f'%(a,b))
a = -1.000000 and b = -0.223400
like image 81
Odomontois Avatar answered Oct 13 '22 14:10

Odomontois