Use a fixed-point format specifier inside the f-string’s braces: f"{value:.2f}" displays two digits after the decimal point. It adds trailing zeroes when needed, so 7.1 becomes 7.10. The result is formatted text; it does not change the original number.
Use .nf for a fixed number of decimal places
The general pattern is f"{number:.nf}", where n is the number of digits to show after the decimal point. The f selects fixed-point formatting, and the precision after the dot specifies the fractional digits.
number = 7.1
print(f"{number:.0f}") # 7
print(f"{number:.2f}") # 7.10
print(f"{number:.4f}") # 7.1000
A precision of zero still uses fixed-point formatting, but displays no digits after the decimal point. For two places, for example, the official Python tutorial uses {math.pi:.3f} to display pi as 3.142. See the Python 3.14.7 tutorial on input and output.
Set the number of places dynamically
If the required precision is stored in a variable, put a nested replacement field in the format specification:
#1 Best Overall
value = 12.34567
places = 3
formatted = f"{value:.{places}f}"
print(formatted) # 12.346
This uses the value of places as the precision. PEP 498 documents expressions in format specifications, including dynamic precision: PEP 498 – Literal String Interpolation.
Choose decimal places, not significant digits
Use .nf when the output must have exactly n digits after the decimal point. The .ng form instead specifies significant digits, and may switch to scientific notation depending on the value and precision.
Rank #2
| Format | What precision counts | Example output for 1234.567 |
|---|---|---|
.2f |
Digits after the decimal point | 1234.57 |
.2g |
Significant digits | 1.2e+03 |
For fixed decimal places, choose f. Use g when significant-digit precision—and notation that can adapt to magnitude—is what you need. Python’s format specification reference defines these precision rules.
Formatting rounds the display, not the stored value
Formatting creates a string representation; it does not overwrite the input variable. For floating-point fixed-point formatting, Python uses a rounding mode that matches the built-in round(), as documented in the built-in types reference.
Recommended Free Tools
Also, a displayed value such as 12.35 does not prove that the underlying binary floating-point value is exact to two decimal places. For financial or other decimal-sensitive calculations, choose a suitable numeric representation and calculation-rounding policy; use the f-string specifier to control the final display. Decimal values can use the same f-string format-specification mechanism, as shown in PEP 498.
Format percentages without confusing display and scaling
To show a value that is already expressed in percentage points, format it and append the percent sign:
percentage = 12.5
print(f"{percentage:.2f}%") # 12.50%
If the value is instead a fraction such as 0.125, decide whether it should be displayed as 0.13% or scaled to 12.50%. Scaling and formatting are different operations. The percent presentation type scales the value for percentage display; use it when that behavior is intended. The Python tutorial’s percentage example multiplies the value by 100 before formatting it with two decimal places.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
The Tool Desk
Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →




