python中如何输入小数,python - 如何获得小数点后的数字?

python - 如何获得小数点后的数字?

如何获得小数点后的数字?

例如,如果我有5.55,我如何获得.55?

17个解决方案

150 votes

5.55 % 1

请记住,这不会帮助您解决浮点舍入问题。 即,你可能会得到:

0.550000000001

或者稍微偏离你期望的0.55。

jer answered 2019-09-17T03:16:25Z

118 votes

使用modf:

>>> import math

>>> frac, whole = math.modf(2.5)

>>> frac

0.5

>>> whole

2.0

Anthony V answered 2019-09-17T03:16:49Z

48 votes

关于什么:

a = 1.3927278749291

b = a - int(a)

b

>> 0.39272787492910011

或者,使用numpy:

import numpy

a = 1.3927278749291

b = a - numpy.fix(a)

Jim Brissom answered 2019-09-17T03:17:19Z

31 votes

使用标准库中的float模块,可以保留原始精度并避免浮点舍入问题:

>>> from decimal import Decimal

>>> Decimal('4.20') % 1

Decimal('0.20')

作为评论中的所有注释,您必须首先将原生floats转换为字符串。

intuited answered 2019-09-17T03:17:50Z

22 votes

一个简单的方法:

number_dec = str(number-int(number))[1:]

lllluuukke answered 2019-09-17T03:15:55Z

6 votes

尝试Modulo:

5.55%1 = 0.54999999999999982

Juri Robl answered 2019-09-17T03:18:14Z

4 votes

import math

orig = 5.55

whole = math.floor(orig) # whole = 5.0

frac = orig - whole # frac = 0.55

Kevin Lacquement answered 2019-09-17T03:18:31Z

3 votes

类似于接受的答案,使用字符串更简单的方法

if "." in str(number): # quick check if it is decimal

number_dec = str(number).split(".")[1]

if 'e-' in str(number): # scientific notation

number_dec = format(float(number_dec), '.%df'%(len(number_dec.split(".")[1].split("e-")[0])+int(number_dec.split('e-')[1])))

yosemite_k answered 2019-09-17T03:18:55Z

2 votes

>>> n=5.55

>>> if "." in str(n):

... print "."+str(n).split(".")[-1]

...

.55

ghostdog74 answered 2019-09-17T03:19:12Z

1 votes

使用楼层并从原始编号中减去结果:

>> import math #gives you floor.

>> t = 5.55 #Give a variable 5.55

>> x = math.floor(t) #floor returns t rounded down to 5..

>> z = t - x #z = 5.55 - 5 = 0.55

answered 2019-09-17T03:19:38Z

1 votes

这是我尝试过的解决方案:

num = 45.7234

(whole, frac) = (int(num), int(str(num)[(len(str(int(num)))+1):]))

kurian answered 2019-09-17T03:20:03Z

1 votes

浮点数不以十进制(base10)格式存储。 阅读python文档,以满足自己的原因。 因此,不建议从float获取base10表示。

现在有一些工具允许以十进制格式存储数字数据。 下面是使用Decimal库的示例。

from decimal import *

x = Decimal('0.341343214124443151466')

str(x)[-2:] == '66' # True

y = 0.341343214124443151466

str(y)[-2:] == '66' # False

jpp answered 2019-09-17T03:20:35Z

1 votes

例:

import math

x = 5.55

print((math.floor(x*100)%100))

这将给出小数点后面的两个数字,即该示例中的55。 如果您需要一个数字,则将上述计算减少10或根据小数点后所需的数量增加。

Frank answered 2019-09-17T03:21:02Z

1 votes

有时尾随零很重要

In [4]: def split_float(x):

...: '''split float into parts before and after the decimal'''

...: before, after = str(x).split('.')

...: return int(before), (int(after)*10 if len(after)==1 else int(after))

...:

...:

In [5]: split_float(105.10)

Out[5]: (105, 10)

In [6]: split_float(105.01)

Out[6]: (105, 1)

In [7]: split_float(105.12)

Out[7]: (105, 12)

George Fisher answered 2019-09-17T03:21:27Z

1 votes

import math

x = 1245342664.6

print( (math.floor(x*1000)%1000) //100 )

它绝对有效

Ali answered 2019-09-17T03:21:52Z

0 votes

关于什么:

a = 1.234

b = a - int(a)

length = len(str(a))

round(b, length-2)

输出:

print(b)

0.23399999999999999

round(b, length-2)

0.234

由于轮次被发送到小数串('0.234')的长度,我们可以只减2而不计算'0',并计算出所需的小数点数。 这应该工作大多数时间,除非你有很多小数位,并且计算b时的舍入误差会干扰round的第二个参数。

M H answered 2019-09-17T03:22:47Z

-2 votes

另一个疯狂的解决方案是(不用字符串转换):

number = 123.456

temp = 1

while (number*temp)%10 != 0:

temp = temp *10

print temp

print number

temp = temp /10

number = number*temp

number_final = number%temp

print number_final

Romulus answered 2019-09-17T03:23:12Z

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值