请教一下大佬,第14讲课后动动手算年终奖这个。
"""年终奖发放"""#引用数据库及函数
from decimal import *
#定义变量
profit = float(input("请输入今年的利润:"))
#定义函数
#主程序
if 0 < profit <= 100000:
award = Decimal(profit * 0.1)
elif 100000 < profit <= 200000:
award = 100000 * 0.1 + (profit - 100000) * 0.075
elif 200000 < profit <= 400000:
award = 100000 * 0.1 + 100000 * 0.075 + (profit - 200000) * 0.05
elif 400000 < profit <= 600000:
award = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + (profit - 400000) * 0.03
elif 600000 < profit <= 1000000:
award = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + 200000 * 0.03 + (profit - 600000) * 0.015
elif 1000000 < profit:
award = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + 200000 * 0.03 + 400000 * 0.015 + (profit - 1000000) * 0.01
else:
award = 0
print( "今年公司亏损,没有年终奖")
print("应该发放的奖金总数是:", award)
这是我的代码,还存在一些问题。
当利润为77777时,算出来的数是7777.70000000000072759576141834259033203125,请教下,要怎么才能让它正常呢?研究了一晚上没研究出来。
保留小数点后面有效位数就可以了 本帖最后由 小甲鱼的铁粉 于 2021-10-15 08:25 编辑
数字在计算机中存储使用的是二进制,讲浮点型数字转化为二进制时,有可能会不精确,导致这个浮点数小数位会特别长
保留小数点后两位就可以了
"""年终奖发放"""
#引用数据库及函数
from decimal import *
#定义变量
profit = float(input("请输入今年的利润:"))
#定义函数
#主程序
if 0 < profit <= 100000:
award = Decimal(profit * 0.1)
elif 100000 < profit <= 200000:
award = 100000 * 0.1 + (profit - 100000) * 0.075
elif 200000 < profit <= 400000:
award = 100000 * 0.1 + 100000 * 0.075 + (profit - 200000) * 0.05
elif 400000 < profit <= 600000:
award = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + (profit - 400000) * 0.03
elif 600000 < profit <= 1000000:
award = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + 200000 * 0.03 + (profit - 600000) * 0.015
elif 1000000 < profit:
award = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + 200000 * 0.03 + 400000 * 0.015 + (profit - 1000000) * 0.01
else:
award = 0
print( "今年公司亏损,没有年终奖")
print("应该发放的奖金总数是:%.2f" % award)
print("应该发放的奖金总数是:{:.2f}".format(award)) 为什么复杂化?
def bonus(amount):
interest = {1000000: 0.01, 600000: 0.015, 400000: 0.03, 200000: 0.05, 100000: 0.075, 0: 0.1}
arr = []
for m in interest.keys():
if amount >= m and m:
bal = amount-m
arr.append(bal*interest)
amount = amount-bal
elif not m:
arr.append(amount*interest)
return round(sum(arr), 2)
print(bonus(77777))7777.7
页:
[1]