View Full Version : python floats and my sister

larswik

Apr 1, 2009, 09:21 PM

Hello. I am trying to write a compound interest program to show my sister what she will owe her attorney in 5 years when she inherits her money from the estate. She owes her lawyer $12,000 and is refusing to pay him. I wrote the program fine but I am having a problem with decimal points.

My 2 questions.

1.

interest = float(raw_input("Enter interest rate: "))

// I want someone to type something in like 18.5. But in order to do the math right I have to type in .185. How can I fix this so it will treat an entry of 18.5% as .185% for the math? Math would be 12000 * .185 / 365 which gives me the daily interest rate.

2.

When the computer prints the dollar amount it prints $12006.0821918.

How can I get it to not print all the extra numbers? I just want $12006.08.

I am guessing it will round up to the nearest penny.

Thanks!

-Lars

nerd

Apr 1, 2009, 10:04 PM

1. Can you not just change 365 to 36500 in your code?

2. You need to apply formatting...I'm not a python person, but try this:

Instead of: print x

Use: print "%0.2f" % x

Good Luck.

(I didn't just do your homework did I?)

eddietr

Apr 1, 2009, 10:21 PM

// I want someone to type something in like 18.5. But in order to do the math right I have to type in .185. How can I fix this so it will treat an entry of 18.5% as .185% for the math?

Did you just ask how to divide a number by 100? :)

How would you divide 18.5 by 100 in Python or any other language?

larswik

Apr 2, 2009, 12:39 AM

Sorry, I am still learning python. Let me print my code for you to see and test. I am sure I am writing this the long way so bare with me.

#Christels interest Calculator

amount = 0

interest = 0

the_days = 0

def owed():

global amount

amount = 0

amount = float(raw_input("Enter what the Loan Amount you owe: "))

return amount

def rate():

global interest

interest = 0

interest = float(raw_input("Enter interest rate: "))

return interest

def months():

global the_days

day = 0

day = int(raw_input("Compounding interest over how many months: "))

the_days = day * 30

return the_days

# main

print owed()

print rate()

print months()

raw_input("Press Return and I will tell you how much it will be at the end:")

while the_days > 0:

# This gets your per day interest rate

today = amount * interest / 365

# This is the amount and interest per day

new_amount = amount + today

the_days -= 1

amount = new_amount

print "Day: ",the_days," Amount: ", new_amount, " daily interest: ", today

print"\t All Done: ", amount

I first ask the person 3 question. Loan Amount, interest rate and length of the loan. They would type in Loan '12,300'... Interest '18.5' and length '60' months, for example. But when I take the users inputs and do the math like, 12300 * 18.5 I come up with a total of 227,500. What I am trying to do is convert the 18.5 which they enter and have that .185. so it would it would look like this 12300 * .185. This would get me 2,275.5 for the annual interest rate. I was then dividing the annual interest rate by 365 days a year to find out what the interest rate was per day.

Again I am new to this so I hope I explained it OK, sorry if I am confusing.

-Lars

plinden

Apr 2, 2009, 12:48 AM

Is your main object to learn python or to show your sister the result of $12,000 left with compound interest over 5 years?

If the latter, just use a compound interest calculator - like http://www.moneychimp.com/calculator/compound_interest_calculator.htm

Depending on how often the 18.5% interest is applied, the result after five years is between $28,039.68 if interest is added annually, and $30,255.33 if interest is applied daily.

By the way 18.5% yearly compound interest is not (18.5/365) % daily. It's 100x(10^(log(1.185)/365) - 1) % ... or 100x(e^(ln(1.185)/365) - 1) %

ChrisA

Apr 2, 2009, 01:23 AM

When the computer prints the dollar amount it prints $12006.0821918.

How can I get it to not print all the extra numbers? I just want $12006.08.

I am guessing it will round up to the nearest penny.

I'm guessing it will simply truncate the number. You will have to do the rounding up. The "trick" is to add half a cent then truncate.

Sander

Apr 2, 2009, 07:52 AM

By the way 18.5% yearly compound interest is not (18.5/365) % daily. It's 100x(10^(log(1.185)/365) - 1) % ... or 100x(e^(ln(1.185)/365) - 1) %

You would think that.

When talking to some financial guy during our mortgage negotiations, it turned out that the above formula is a little too complex for banks. To calculate the amount of interest owed after a partial year, they actually use the formula real_intrest*(number of days)/360. And no, that 360 is not a typo.

Everytime I remember this incident, I have to try really hard not to be condescending towards financial types.

eddietr

Apr 2, 2009, 10:25 AM

What I am trying to do is convert the 18.5 which they enter and have that .185. so it would it would look like this 12300 * .185.

Well, you have 18.5 and you want 0.185, then you simply have to divide by 100. And you know how to divide by 100 in python, because you have already divided by 365 elsewhere.

As others have pointed out, you are not compounding the interest correctly. But I would get this working first and then worry about fixing that bug. That itself is a learning exercise.

Good luck and have fun with this.

larswik

Apr 2, 2009, 11:43 AM

Plinden - I am not learning Python for my sister. I am learning it because I want to do motion control and iPhone apps. I started with Python because it is an easier language to start programing with. I will be switching to Objective-C when done. - Also I am writing it to try to learn and challenge myself.

ChrisA- I don't know how to truncate yet?

eddietr- That makes sense. Plinden's math boggles my mind. I would assume that compound interest happens everyday. So what ever interest accrued yesterday is added to the total today and then the new interest rate is calculated on the new total today. Is that no compounding interest?

Thanks folks!

-Lars

Sander

Apr 3, 2009, 12:32 PM

I would assume that compound interest happens everyday. So what ever interest accrued yesterday is added to the total today and then the new interest rate is calculated on the new total today. Is that no compounding interest?

Yes. The point is to figure out what the daily interest is. You mostly see the yearly interest quoted, and the proper way to get the daily interest is by taking its 365th root (but that's not how banks do it - at least not here in The Netherlands).