Novice · Formatting · 51 of 264
Big money
Read an amount of money and print it with a comma between thousands and exactly two decimals.
Input: a number (may have decimals). Output: e.g. 1,234,567.50 Tip: f"{x:,.2f}"
Examples
Input
1234567.5
Output
1,234,567.50
Input
999
Output
999.00
Input
0.1
Output
0.10
One more test checks other inputs when you press Check.
Stuck? The idea behind formatting
An f-string puts values into text: f"{name} is {age}". Inside the braces, a format after a colon controls how the value looks.
:.2f gives two decimal places, :5 makes room for five characters, :>5 lines up to the right, :05 fills with zeros.
price = 3.5
print(f"Total: £{price:.2f}")
print(f"{7:03}") # 007Watch for: round() can drop a trailing zero (3.50 becomes 3.5); :.2f always shows two places.