'How to round a float string inside a format specifier print statement
I'm using a format specifier inside a print statement in python to print the output as columns. I have a float variable which I would like to round off to two decimal palaces without using the round function within the print statement. How can I go about doing this?
price = 1.25 * 3.55 #4.4375
print('{:<15}{:<15}{:<15.2f}'.format('Panadol','2 Units', price))
# I need the output to be
# Panadal 2 units 4.44
The above piece of code throws an error stating that this is an invalid format specifier. How to achieve the above output?
Edit : Pasting the error message
***Run error***
Traceback (most recent call last):
File "__tester__.python3", line 33, in <module>
print('{:>15}{:>15}{:>15}{:>15.2f}'.format(apple_item_name,apples_weight,apple_price_pink_lady,str(total_price_of_apples)))
ValueError: Unknown format code 'f' for object of type 'str'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
