使用两个输入将两个输入转换为货币

时间:2014-11-17 03:14:13

标签: decimal python-3.3

如何简单地改变我的输入,并在它们后面加上两位小数?

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

这时我运行程序:

>>> 
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 个答案:

答案 0 :(得分:2)

这似乎是使用decimal.Decimal的好时机。 quick-start tutorial是概述模块的好地方。只需将货币值更改为decimal.Decimal个对象,将精度设置为两位数,即可全部设置。

答案 1 :(得分:1)

simple替换为"{:.2f}".format(simple)等。这是a good reference