round(value,2)的作用是什么?

2024-09-30 22:13:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在参加麻省理工开放式课程的免费在线课程。我被赋予了一个特定的问题,我正在努力理解解决办法。我不知道“2”在做什么。它是(minMonthlyPaymentRate*balance,2)中的两个,下面是解决方案:

# 6.00 PS1-A Solution
# Determines remaining credit card balance after a year of making the minimum payment each month

balance = float(raw_input("Enter the outstanding balance on your credit card: "))
annualInterestRate = float(raw_input("Enter the annual credit card interest rate as a decimal: "))
minMonthlyPaymentRate = float(raw_input("Enter the minimum monthly payment rate as a decimal: "))

# Monthly Interest Rate
monthlyInterestRate = annualInterestRate/12

# Initialize state variables
numMonths = 1
totalAmtPaid = 0

while numMonths <= 12:
    # Minimum monthly payment of balance at start of the month
    minPayment = round(minMonthlyPaymentRate * balance,2)**This Two!?**
    totalAmtPaid += minPayment

    # Amt of monthly payment that goes to interest
    interestPaid = round(monthlyInterestRate * balance,2)

    # Amt of principal paid off
    principalPaid = minPayment - interestPaid

    # Subtract monthly payment from outstanding balance
    balance -= principalPaid

    print "Month:", numMonths
    print "Minimum monthly payment:", minPayment
    print "Remaining balance:", balance

    # Count this as a new month     
    numMonths += 1

print "RESULT"
print "Total amount paid:",totalAmtPaid
print "Remaining balance:",balance

Tags: oftheinputrawpaymentfloatcardprint
3条回答

round()舍入到给定的位数并返回浮点数。你知道吗

示例代码:

val=2.665
print(round(val, 2);

output:
6.67

一张图片胜过千言万语。

enter image description here

python documentation of the function round()所示,它表示“在小数点后舍入到ndigits位”。所以它意味着结果将四舍五入到小数点后的两位数。你知道吗

相关问题 更多 >