如何使它产生漂亮的列,但保持小数点后两位?

时间:2019-05-13 21:05:12

标签: python-3.x

我试图在一个表中产生两列数据,这些表也具有一个带小数点后两位的浮点输出。我既可以执行任何一项操作,也可以执行当前操作,但不能同时执行两项操作。如何简单地将这两种格式结合起来?


#Code that shows 2 decimal places

def c2f():
    print("Celsius","Fahrenheit")
    for i in [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]:
        c = i
        f = 9 / 5 * i + 32
        table_data = [
            [c,f], 
        ]
        for row in table_data:
            print("{: .2f} {: .2f}".format(*row))
c2f()

#Code that formats data into columns

def c2f():
    print("Celsius","Fahrenheit")
    for i in [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]:
        c = i
        f = 9 / 5 * i + 32
        table_data = [
            [c,f], 
        ]
        for row in table_data:
            print("{: <20} {: <20}".format(*row))
c2f()

1 个答案:

答案 0 :(得分:0)

我个人使用此:

print("{:9}|".format("%.2f" % 12))

输出:

12.00    |