'How to display items from a list as having 2 decimal places, not 1 [duplicate]
Whenever I print out anything from the lists at the top, it is always displayed as having only one decimal place, e.g. oneAdult[0] is displayed as 20.0 not 20.00 like I want it to. I want it to be the latter as it is a currency, so just one decimal place would look weird. Thanks for help!
oneAdult = [20.00, 30.00]
oneChild = [12.00, 18.00]
oneSenior = [16.00, 24.00]
familyTicket = [60.00, 90.00]
sixPeoplePlus = [15.00, 22.50]
print("this tells you information about prices of one and two day tickets")
while True:
try:
oneOrTwo = int(input("are you buying tickets for 1 or 2 days? "))
if oneOrTwo == 1:
print("day succesfully selected. the prices are,\n for one adult $", oneAdult[0], "\n for one child $", oneChild[0], "\n for one senior $", oneSenior[0], "\n for a family ticket $",familyTicket[0], "\n and for a group of six people or more(price per ticket) $",sixPeoplePlus[0])
break;
elif oneOrTwo == 2:
print("the prices are,\n for one adult $", oneAdult[1], "\n for one child $", oneChild[1], "\n for one senior $", oneSenior[1], "\n for a family ticket $",familyTicket[1], "\n for a group of six people or more(price per ticket) $",sixPeoplePlus[1])
break;
else:
print("your answer needs to be either '1' or '2'")
except ValueError:
print("provide this value in integer form")
continue
print("\n")
Solution 1:[1]
You could use the string formatting operator for that:
>>> '%.2f' % 1.234
'1.23'
>>> '%.2f' % 5.0
'5.00'
The result of the operator is a string, so you can store it in a variable, print etc.
Solution 2:[2]
If you want to show 2 decimal place in the prices then just add "%0.2f" %oneAdult[0]
like this
print("day succesfully selected. the prices are,\n for one adult $ %0.2f" %oneAdult[0])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rachit Kumar Pandey |
| Solution 2 | Rakesh Kr Yadav |
