Cam'ron
Cam'ron

Reputation: 91

Output is not formatting correctly

Writing a function to convert Celsius to Fahrenheit. I must find the Fahrenheit equivalent to each Celsius value and print both side by side in a two column chart. The numbers MUST BE FORMATTED TO ONE(1) OR TWO(2) DECIMAL PLACES. Everything works except the format to two decimal places.

    def temp():
    g = range(0, 100)
    for temp in g:
        F = (9 / 5) * (temp) + (32)
        print("{:.2f}". format(F))    
        print(f'{float(temp)} {float(F):>25}')
    return F
print(temp())

.The last few lines(all the other lines are functioning except the last)

98.0        208.40                     
210.20      208.4
99.0        210.20000000000002
210.20000000000002

how do I get the last number from both columns to have only 1 or 2 decimal places

Upvotes: 0

Views: 88

Answers (3)

Fatih Atalay
Fatih Atalay

Reputation: 143

You can use

F = round((9 / 5) * (temp) + (32), 2)

Upvotes: 1

Blubywaff
Blubywaff

Reputation: 26

Using print(f'{float(temp)} {float(F):>25.2f}') to replace the 2nd print statement should accomplish this. And to do the same thing on the last line on the right you could replace the final print with print(f'{temp():>25.2f}').

the .2f rounds to 2 decimal places like you have in the first print.

See the python PEP docs for more info on string formatting.

Upvotes: 1

Clay Shwery
Clay Shwery

Reputation: 380

Use round

Change last line to return round(F,2)

Upvotes: 0

Related Questions