How to Format Numbers in a Python Log Message with %d and %.2f
Log messages often carry numbers: how many rows were read, how long a step took, what an error measured. Python's logging fills them into the message from placeholders, and %d and %.2f decide how they look: a whole number, or a number with exactly two decimals. This guide logs both on Python 3.15.2.
Syntax
logger.info("<text> %d <text> %.2f <text>", <whole number>, <number>)
| Part | What it means |
|---|---|
%d | A whole number. A value with decimals is cut to its whole part |
%.2f | A number with two decimals; %.4f gives four |
| The values after the message | One per placeholder, in order. logging fills them in only when it shows the line |
Python's logging tutorial explains that "merging of variable data into the event description message uses the old, %-style of string formatting". The printf-style formatting table lists d as "Signed integer decimal." and f as "Floating-point decimal format."
The Example
A shop logs a sale: 3 bikes for 1349.5 pounds. The count is a whole number, and the money needs two decimals.
Prerequisites
- uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
import logging
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s")
logger = logging.getLogger(__name__)
logger.info("Sold %d bikes for %.2f pounds", 3, 1349.5)

Step 2: Run It
uv run python log_numbers.py
INFO Sold 3 bikes for 1349.50 pounds

%d showed 3, and %.2f showed 1349.5 as 1349.50, with the second decimal added. The command works unchanged in PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
%d for a value with decimals | The decimals are cut off, without a warning: 3.9 logged as Sold 3 bikes | Use %.2f, or %s to show the value as it is |
%d for text: logger.info("Sold %d bikes", "three") | The program goes on, but logging prints --- Logging error --- and TypeError: format argument 1: %d requires a real number, not str instead of the line | Pass a number, or use %s |
Formatting the message yourself: logger.info(f"Sold {count} bikes") | Works, but the text is built even when the level hides the line. The logging tutorial says "Formatting of message arguments is deferred until it cannot be avoided" only when the values come after the message | Pass the values after the message |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
--- Logging error --- with %d requires a real number | A %d placeholder got text | Pass a number, or switch to %s |
| A logged number has lost its decimals | %d cut them off | Use %.2f |
FAQ
What does the . in %.2f mean?
It starts the precision. The formatting table describes it as "Precision (optional), given as a '.' (dot) followed by the precision": for f, the number of decimals. %.2f shows two, %.4f four.
Why does logging use % and not f-strings?
For backwards compatibility. The logging tutorial says of the %-style: "This is for backwards compatibility: the logging package pre-dates newer formatting options such as str.format() and string.Template."
Conclusion
logger.info("Sold %d bikes for %.2f pounds", 3, 1349.5) logged Sold 3 bikes for 1349.50 pounds. Use %d for counts, %.2f for amounts, and %s when you're not sure.
Additional Resources
- Logging variable data — the logging tutorial.
- printf-style String Formatting — every placeholder and flag.
