python怎么设置输出格式_如何在Python中将输出格式设置为两位小数?

我试图在Python中将输出格式化为两个小数位..这是我的代码def introduction():

print("This calculator calculates either the Simple or Compound interest of an given amount")

print("Please enter the values for principal, annual percentage, number of years, and number of times compounded per year")

print("With this information, we can provide the Simple or Compound interest as well as your future amount")

def validateInput(principal, annualPercntageRate, numberOfYears,userCompound):

if principal < 100.00:

valid = False

elif annualPercntageRate < 0.001 or annualPercntageRate > .15:

valid = False

elif numberOfYears < 1:

valid = False

elif userCompound != 1 and userCompound != 2 and userCompound != 4 and userCompound != 6 and userCompound != 12:

valid = False

else:

valid = True

return valid

def simpleInterest(principal, annualPercentageRate, numberOfYears):

return (principal * annualPercentageRate * numberOfYears)

def compoundInterest(principal, annualPercentageRate, numberOfYears, userCompound):

return principal * ((1 + (annualPercentageRate / userCompound))**(numberOfYears * userCompound) - 1)

def outputAmounts(principal, annualPercentageRate, numberOfYears, userCompound, simpleAmount,compoundAmount):

print("Simple interest earned in", numberOfYears, "will be $",simpleAmount,"making your future amount $",(principal + simpleAmount)

print("Interest compounded", userCompound, "in",numberOfYears, "will earn $",compoundAmount,"making your future amount",(principal + compoundAmount)

def main():

introduction()

principal = float(input("Enter principal: "))

annualPercentageRate = float(input("Enter rate: "))

numberOfYears = int(input("Enter years: "))

userCompound = int(input("Enter compounding periods: "))

if validateInput(principal, annualPercentageRate, numberOfYears, userCompound):

simpleAmount = simpleInterest(principal, annualPercentageRate, numberOfYears)

compoundAmount = compoundInterest(principal, annualPercentageRate, numberOfYears, userCompound)

outputAmounts(principal, annualPercentageRate, numberOfYears, userCompound, simpleAmount,compoundAmount)

else:

print("Error with input, try again")

main()

所以对于我的输出,我想将结尾格式化为两个小数位。也就是说,这两个变量

-(委托人+合作方)

-(委托人+simpleAmount)

我知道我需要使用.2,但我不确定如何将它添加到print语句中,以便它输出到小数点后两位…我该如何做?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值