Py||除法训练(保留小数点后指定位数不足补零)

题目描述
对三个整数a,b,c,求a÷b的值,结果保留c位有效数字,c>=0 且 c<=1000

输入
输入包括多行数据,每行是用空格分隔的三个整数a b c,其中c>=0 且 c<=1000。

输出
对输入的每行数据,输出包括一行,a÷b的保留c位有效数字的结果,如果b等于0,该行输出error

样例输入 Copy
1 2 1
1 2 0
0 2 2
样例输出 Copy
0.5
1
0.00

from decimal import Decimal
while True:
    a,b,c=map(float,input().split())
    if b!=0:
        d=a/b
        c=int(c)
        a = Decimal(str(d))
        a = round(a,c)
        print(a)
    elif b==0:
        print('error')

如果不用补零

while True:
    a,b,c=map(float,input().split())
    if b!=0:
        d=a/b
        c=int(c)
        if c!=0:
            print((int(d*10.0**c+0.5))/10.0**c)
        else:
            print((int(d*10.0**c+0.5))//10**c)
    elif b==0:
        print('error')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值