Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk2 min

How to Format Decimal Places in Python Using f-Strings

Use an f-string format specifier such as f"{value:.2f}" to display a number with two decimal places, including trailing zeroes.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.