在Python中,我如何分割一个数字的小数点 [英] in python, how do i split a number by the decimal point

查看:401
本文介绍了在Python中,我如何分割一个数字的小数点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,如果我跑:

  a = b / c 

并得到结果 1.2234



我分开它,以便我有:

pre code a = 1
b = 0.2234

解决方案

 >>> from math import modf 
>>> b,a = modf(1.2234)
>>> print('a =%f和b =%f'%(a,b))
a = 1.000000和b = 0.223400
>>> b,a = modf(-1.2234)
>>> print('a =%f和b =%f'%(a,b))
a = -1.000000和b = -0.223400


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

解决方案

>>> 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

这篇关于在Python中,我如何分割一个数字的小数点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆