0

I am trying to produce two columns of data in a table that also have a float output with 2 decimal places. I can do either or at the moment but not both. How can I simply combine the formats of these two?


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

  • 2
    Possible duplicate of [How to format a floating number to fixed width in Python](https://stackoverflow.com/questions/8885663/how-to-format-a-floating-number-to-fixed-width-in-python) – mkrieger1 May 13 '19 at 21:09

1 Answers1

0

I personally use this:

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

output :

12.00    |
Sh3mm
  • 58
  • 5