
和round()
来计算复利终值。
以下是一个示例代码:
def compound_interest(principal, rate, time):
"""
计算复利终值
:param principal: 本金
:param rate: 年利率
:param time: 年数
:return: 复利终值
"""
ci = principal * pow((1 + rate / 100), time)
ci = round(ci, 2)
return ci
其中,principal
是本金,rate
是年利率,time
是年数。该函数中使用pow()
计算了复利,使用round()
对结果保留两位小数。你可以基于此函数自行修改,以适应你的具体场景。
定义 HTML 页面中的图像
以下是Python代码实现复利终值计算的示例:
def compound_interest(principal, rate, time):
"""
This function calculates the compound interest for given principal, rate and time.
"""
amount = principal * (1 + rate/100) ** time
return amount
# Example usage
principal = 1000
rate = 5
time = 2
amount = compound_interest(principal, rate, time)
print(f"Principal: {principal}, Rate: {rate}, Time:{time}, Amount:{amount}")
该函数接受三个参数:本金,利率和时间。使用给定的公式,计算出复利终值并返回。在这个例子中,我们使用1000作为本金,5%的利率和2年的时间,计算出复利终值并将其打印出来。
如果你想要计算未来每年的复利终值,可以使用以下代码:
def compound_interest(principal, rate, time):
"""
This function calculates the compound interest for given principal, rate and time.
"""
total = principal
for i in range(time):
total = total * (1 + rate/100)
print(f"Year {i+1} total amount: {total}")
return total
# Example usage
principal = 1000
rate = 5
time = 3
amount = compound_interest(principal, rate, time)
print(f"Principal: {principal}, Rate: {rate}, Time:{time}, Amount:{amount}")
该函数使用一个循环,计算未来每年的复利终值并打印输出。在这个例子中,我们使用1000作为本金,5%的利率和3年的时间,计算未来每年的复利终值并将其打印出来。最后,它返回总的复利终值并将其打印出来。