-2

I'm trying to format my output into 2 decimal places in Python..This is my code

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()

So for my output, I want to format the ending to 2 decimal places. Namely, these 2 variables -(principal + compoundAmount) -(principal + simpleAmount)

I know I need to use %.2, but Im not sure how to add that into a print statement so that it would output into 2 decimal places...How do I do this?

Dark Matter
  • 190
  • 2
  • 13
Mohan Aravind
  • 111
  • 2
  • 2
  • 5
  • 3
    To get a good answer, please do the work of editing your post to be easily readable. – Chris Mar 02 '16 at 04:30
  • 3
    Possible duplicate of [Limiting floats to two decimal points](http://stackoverflow.com/questions/455612/limiting-floats-to-two-decimal-points) – YBathia Mar 02 '16 at 04:31

2 Answers2

5

try this

print('pi is {:.2f}'.format(your_variable))
sunny
  • 178
  • 3
  • 12
2

You just need simple formatting string, like: print('pi is %.2f' % 3.14159) which output is pi is 3.14

You might wanna read https://docs.python.org/2.7/library/string.html#formatspec

Fingalzzz
  • 109
  • 14
  • The docs you linked don't actually apply to `%` based `printf`-like formatting. They're for the more powerful/flexible formatting with `str.format`. The `str.format` approach would be `'pi is {:.2f}'.format(3.14159)`. Not better or worse in this case, but `str.format` is extensible to user defined types. – ShadowRanger Mar 02 '16 at 04:48