Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python - Rounding and Formatting Numbers
## INTEREST CALCULATOR ##
Lesson("Interest Calculator (Challenge)")
## The formula is A = P(1+r/n)**(n*t)
## Create each variable separate
## P = Principal, r = Rate %, n = times compounded per year, t = years
## find How much money you will have if you invest your college tuition, $100,000 into a investment account for 40 years earning 7% interest compounded yearly.
##CREATE HERE##
P = 100000
t = 40
r = .07
n = 1
A = P*(1+r/n)**(n*t)
B = round(A,2) # https://gist.github.com/jackiekazil/6201722
C = '{:,}'.format(B)
# to format output with comma for thousands separator:
# http://stackoverflow.com/questions/1823058/how-to-print-number-with-commas-as-thousands-separators/10742904#10742904
## STOP HERE ##
##uncomment the print line when you are done and run the script. The correct answer should be $1,497,445.78
print("You invested $%s in 2016. \nOver %s years you earned on average %1.2f percent interest.\nYou are now retired with $%s." %(P, t, r*100, C))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.