Skip to main content

How to Format Numbers in a Python Log Message with %d and %.2f

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
logger.info("<text> %d <text> %.2f <text>", <whole number>, <number>)
PartWhat it means
%dA whole number. A value with decimals is cut to its whole part
%.2fA number with two decimals; %.4f gives four
The values after the messageOne 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​

log_numbers.py
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)

The log_numbers.py script in a macOS-style code window: logging.basicConfig with a short format, a logger, and logger.info("Sold %d bikes for %.2f pounds", 3, 1349.5).

Step 2: Run It​

Terminal
uv run python log_numbers.py
Output
INFO Sold 3 bikes for 1349.50 pounds

The terminal after 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​

MistakeWhat happensDo this instead
%d for a value with decimalsThe decimals are cut off, without a warning: 3.9 logged as Sold 3 bikesUse %.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 linePass 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 messagePass the values after the message

Troubleshooting​

SymptomLikely causeFix
--- Logging error --- with %d requires a real numberA %d placeholder got textPass a number, or switch to %s
A logged number has lost its decimals%d cut them offUse %.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​