python求小数左后各有几位数_查找小数点后的位数

我试图编写一个Python 2.5.4代码来编写一个函数,该函数以一个浮点数x作为输入,并返回x中小数点后的位数

这是我的代码:def number_of_digits_post_decimal(x):

count = 0

residue = x -int(x)

if residue != 0:

multiplier = 1

while int(multiplier * residue) != (multiplier * residue):

count += 1

multiplier = 10 * multiplier

print count

print multiplier

print multiplier * residue

print int(multiplier * residue)

return count

print number_of_digits_post_decimal(3.14159)

while循环中的print语句仅用于调试目的。

现在,当我运行这段代码时,我得到以下输出。1

10

1.4159

1

2

100

14.159

14

3

1000

141.59

141

4

10000

1415.9

1415

5

100000

14159.0

14158

6

1000000

141590.0

141589

7

10000000

1415900.0

1415899

8

100000000

14159000.0

14158999

9

1000000000

....

此函数返回的count的最终值是17。

如何修改此代码以达到预期的结果?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值