我正在尝试完成一个任务,但是Python总是将我的答案向下取整而不是向上取整,这是不正确的。以下是我的代码:
startingValue = int(input())
RATE = float(input()) /100
TARGET = int(input())
currentValue = startingValue
years = 1
print("Year 0:",'$%s'%(str(int(startingValue)).strip() ))
while years <= TARGET :
interest = currentValue * RATE
currentValue += interest
print ("Year %s:"%(str(years)),'$%s'%(str(int(currentValue)).strip()))
years += 1
这是我的代码输出结果: 第0年:$10000, 第1年:$10500, 第2年:$11025, 第3年:$11576, 第4年:$12155, 第5年:$12762, 第6年:$13400, 第7年:$14071, 第8年:$14774, 第9年:$15513,
这是预期的输出结果: 第0年:$10000, 第1年:$10500, 第2年:$11025, 第3年:$11576, 第4年:$12155, 第5年:$12763, 第6年:$13401, 第7年:$14071, 第8年:$14775, 第9年:$15514,
我需要它们匹配,即四舍五入。请有人帮帮我:(
int
默认向下取整。看起来你需要四舍五入到最近的整数:https://dev59.com/A1wZ5IYBdhLWcg3wINTQ - milancurcicmath.ceil
。 - John Coleman