convert two inputs into monetary with two inputs

77 views Asked by At

How do I change my inputs simple and compound with two decimals behind them?

n = 5
rate = 0.05
for n in range(0, 3):
    principal = 10000
    for n in range (0,6):
        principal <= 15000
        simple = principal *  (1 + rate * n)
        compound = principal * (1 + rate)**n
        ratea = rate * 100
        ratea = int(ratea)
        principal = int(principal)
        print((ratea),"% $",principal," $",simple," $",float(compound))
        principal = principal + 1000
    rate = rate + 0.05

Here when I run the program:

>>> 
5 % $ 10000  $ 10000.0  $ 10000.0

5 % $ 11000  $ 11550.0  $ 11550.0

5 % $ 12000  $ 13200.000000000002  $ 13230.0

5 % $ 13000  $ 14949.999999999998  $ 15049.125000000002

5 % $ 14000  $ 16800.0  $ 17017.0875

5 % $ 15000  $ 18750.0  $ 19144.223437500004

10 % $ 10000  $ 10000.0  $ 10000.0

10 % $ 11000  $ 12100.000000000002  $ 12100.000000000002

10 % $ 12000  $ 14400.0  $ 14520.000000000002

10 % $ 13000  $ 16900.0  $ 17303.000000000004

10 % $ 14000  $ 19600.0  $ 20497.400000000005

10 % $ 15000  $ 22500.0  $ 24157.65000000001

15 % $ 10000  $ 10000.0  $ 10000.0

15 % $ 11000  $ 12649.999999999998  $ 12649.999999999998

15 % $ 12000  $ 15600.0  $ 15869.999999999998

15 % $ 13000  $ 18850.000000000004  $ 19771.374999999996

15 % $ 14000  $ 22400.0  $ 24486.08749999999

15 % $ 15000  $ 26250.0  $ 30170.35781249999
2

There are 2 answers

2
DrPositron On BEST ANSWER

Replace simple with "{:.2f}".format(simple), etc. Here's a good reference.

0
MattDMo On

This looks like a great time to use decimal.Decimal. The quick-start tutorial is a great place for an overview of the module. Just change your monetary values to decimal.Decimal objects, set the precision to two digits, and you'll be all set.