

Raw float output is rarely pleasant to read. A value like 123.456789 clutters a bill, a report or a score card, and what the reader needs is 123.46. The %.2f format is one of the oldest ways in Python to fix that, and it still appears in a lot of existing code.
Learn what each part of %.2f means, how to use it for prices and percentages, and how it compares with f-strings, .format() and round(). You’ll also explore precision options like .1f and .3f, along with common mistakes beginners should watch out for. PW Skills also offers Python courses to help you develop these skills further.
%.2f is a format specifier. It tells Python to take a number and show it as a float with exactly two digits after the decimal point.
python
price = 19.9
print('%.2f' % price)
Output:
19.90
The result is a string, not a number. The original variable stays the same, and only the displayed text changes.
|
Part |
Meaning |
|
% |
Marks the start of a format specifier, the place where the value is inserted |
|
.2 |
Sets the precision, which is two digits after the decimal point |
|
f |
Formats the value as a fixed-point floating-point number |
Change the digit after the dot and the number of decimal places changes with it. Before going further, it helps to be clear about variables in Python, since the value you format is usually stored in one.
The % operator sits between the string and the value. When there is more than one value, they go inside parentheses in the same order as the specifiers.
python
price = 19.9
discount = 12.3456789
total = 123.456789
print('Price: %.2f' % price)
print('Discount: %.2f%%' % discount)
print('Total: %.2f' % total)
print('Item: %s, Price: %.2f' % ('Notebook', 49.5))
print('%.2f' % 5)
print('%.2f' % (7 / 3))
Output:
Price: 19.90
Discount: 12.35%
Total: 123.46
Item: Notebook, Price: 49.50
5.00
2.33
A few things are worth noticing here:
19.9 becomes 19.90, because %.2f always shows two decimal places.
123.456789 becomes 123.46, because the value is rounded to two places for display.
To print a literal percent sign, write %% inside the string.
Integers and calculations work too, so 5 becomes 5.00 and 7 / 3 becomes 2.33.
For more on how values reach the screen, see this guide to Python input and output.
Prices and percentages are the most common reason to use two decimal places.
python
amount = 1499.5
rate = 8.3333
print('Amount: Rs. %.2f' % amount)
print('Rate: %.2f%%' % rate)
Output:
Amount: Rs. 1499.50
Rate: 8.33%
Want to build a stronger base in Python before moving on? Learn with PW Skills Data Science Course in Python
Alternative Ways to Format Floats to 2 Decimal Places
Python offers three other routes to two decimal places. Two of them, f-strings and .format(), work like %.2f and return text. The third, round(), behaves differently.
In an f-string, the value goes inside curly braces and the format follows a colon. The .format() method uses the same format, with the value passed in afterwards.
python
value = 123.456789
print(f'{value:.2f}')
print('{:.2f}'.format(value))
print('{0:.2f}'.format(value))
Output:
123.46
123.46
123.46
The part after the colon is .2f, the same instruction as in %.2f without the leading %. All three lines return a string with two decimal places.
The round() function returns a number, not text, so it does not keep a trailing zero.
python
print(round(19.9, 2))
print('%.2f' % 19.9)
print(round(123.456789, 2))
Output:
19.9
19.90
123.46
round(19.9, 2) gives 19.9 because the float 19.9 has no extra digit to show. The formatted version gives 19.90 because the format forces two places. Use round() when the result feeds into further calculations, and use formatting when the result is only displayed.
Want to see where these Python basics lead? Explore PW Skills Data Science with Generative AI course
Compare Python’s common 2-decimal formatting methods to understand how they differ in output, trailing zeros, return types, and when to use each one.
|
Method |
Example |
Return Type |
Trailing Zeros |
Best For |
|
%.2f |
'%.2f' % 19.9 |
str |
Kept (19.90) |
Maintaining older code |
|
f-string |
f'{x:.2f}' |
str |
Kept (19.90) |
New code and readable output |
|
str.format() |
'{:.2f}'.format(x) |
str |
Kept (19.90) |
Reusable templates and older code |
|
round() |
round(x, 2) |
float (int if the input is an int) |
Not kept (19.9) |
Further arithmetic |
The three formatting methods give the same text. The real choice is between text for display and a number for calculation.
Changing the digit after the dot changes how many decimal places appear.
python
x = 123.456789
print('%.1f' % x)
print('%.2f' % x)
print('%.3f' % x)
Output:
123.5
123.46
123.457
Keep these pitfalls in mind:
Strings raise an error. Passing text to %.2f raises a TypeError. Convert numeric text with float() first.
The result is text. A formatted value cannot be used in arithmetic until it is converted back to a number.
Binary floats can surprise you. Computers store many decimal fractions as close approximations, so rounding by hand and rounding in Python can disagree.
Display is not exact arithmetic. Formatting controls how a number looks, so it should not be relied on for exact money calculations.
python
print('%.2f' % 2.675)
print(round(2.675, 2))
Output:
2.67
2.67
Many people expect 2.68 here. The stored value of 2.675 is a close approximation, which is why both lines show 2.67.
python
print('%.2f' % float('19.9'))
Output:
19.90
Passing the plain string 'abc' instead raises TypeError: must be real number, not str. If numeric text is the source, type casting in Python explains how to convert it before formatting. For a wider look at how floats and integers behave, understand numeric types in Python.
Curious where Python skills lead next? Explore free AI and machine learning courses
Use f-strings when writing new code, keep %.2f for older code you maintain, and reach for round() only when you need a number rather than text. Practise each method in a real script or notebook before moving on to libraries such as NumPy and Pandas, where clean number formatting matters in every report.
Ready to apply this in real data projects? Explore PW Skills data science and analytics courses
