2
下面的函數返回:您的月付款爲6.25年529.22美元,首付4620.06美元。Python:將小數轉換爲月份
如何將小數轉換爲4個月而不是.25年。
我希望輸出結果如下:您的月付款爲6年零4個月529.22美元,首付4620.06美元。
def newcar():
input("How much is a new car going to cost you per month? Please hit
enter to start")
p = int(input("Please enter total cost of car: "))
r = float(input("Please enter interest rate as a whole number(example:
15.6% = 15.6): National average is around 10.5%): "))
t = int(input("These payments would last for how many months?: "))
dp = int(input("Please enter the downpayment percentage as a whole
number: example: 20% = 20: "))
afterdp = p - (p * dp/100)
downpay = p - afterdp
downpay = round(downpay, 2)
interest = afterdp * (r/100) * (t/12)
interest = round(interest, 2)
monthly_payment_bt = (afterdp + interest)/t
monthly_payment_bt = round(monthly_payment_bt, 2)
monthly_payment = (monthly_payment_bt * .07) + monthly_payment_bt
monthly_payment = round(monthly_payment, 2)
t = round(t/12, 2)
return("Your monthly payment would be $" + str(monthly_payment) + "
for " + str(t) + " years, and a downpayment of $" + str(downpay))
print(newcar())
只要做'.25 * 12'和'round()'it ...順便說一句,1年的25%等於3個月,而不是4個。 –
我很確定你沒有計算利息正確。通常情況下,每月的利息都是複利的,所以您不會爲已經支付的部分支付利息(但您確實支付過往利息)。我建議使用[這個公式](https://en.wikipedia.org/wiki/Mortgage_calculator#Monthly_payment_formula),並用整數個月(而不是分數)來計算。 – Blckknght