Python
F-strings
Decimal
Programming
Coding

Fixed digits after decimal with f-strings

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Python f-strings, the usual way to show a fixed number of digits after the decimal point is the format specifier .nf, where n is the number of decimal places. For example, f"{value:.2f}" formats a number with exactly two digits after the decimal point and rounds it using Python's normal floating-point formatting rules.

The Basic Pattern

For fixed decimal places, the core syntax is:

python
value = 98.7654321
formatted = f"{value:.2f}"
print(formatted)

Output:

text
98.77

The .2f part means:

  • 'f for fixed-point formatting'
  • '2 for two digits after the decimal point'

This is the form most people need for prices, summaries, table output, and user-facing numeric display.

More Examples with Different Precisions

The same pattern works for any precision you need.

python
1value = 3.14159265
2
3print(f"{value:.0f}")
4print(f"{value:.1f}")
5print(f"{value:.3f}")

Output:

text
3
3.1
3.142

Notice that f formatting always produces exactly that many decimal places. That is different from simply rounding a number in arithmetic and printing it normally.

Width, Alignment, and Leading Zeros

You can combine fixed decimal places with width and alignment.

python
1number = 7.125
2
3print(f"{number:7.2f}")
4print(f"{number:>7.2f}")
5print(f"{number:<7.2f}")
6print(f"{number:07.2f}")

Output:

text
1   7.12
2   7.12
37.12   
40007.12

These options are helpful when you are formatting columns, reports, or filenames with numeric components.

Dynamic Precision

If the number of decimal places must be chosen at runtime, you can place the precision in a variable.

python
1value = 12.345678
2digits = 4
3
4print(f"{value:.{digits}f}")

Output:

text
12.3457

That is useful in configurable reporting tools or reusable formatting helpers.

Floating-Point Formatting Versus Decimal Arithmetic

F-strings format values for display. They do not change how binary floating-point numbers are represented internally. That distinction matters in financial or high-precision code.

python
value = 2.675
print(f"{value:.2f}")

Some outputs surprise people because floating-point numbers cannot represent every decimal exactly. If exact decimal behavior matters, use Decimal.

python
1from decimal import Decimal
2
3price = Decimal("2.675")
4print(f"{price:.2f}")

F-strings still work with Decimal, but now the underlying value behaves more like decimal arithmetic rather than binary floating point.

Formatting in Real Applications

A small helper can keep presentation rules consistent.

python
1def format_price(amount: float) -> str:
2    return f"${amount:.2f}"
3
4
5values = [1, 2.5, 3.999]
6for value in values:
7    print(format_price(value))

Output:

text
$1.00
$2.50
$4.00

That is often cleaner than repeating inline formatting rules throughout a codebase.

Choose the Specifier Intentionally

f is for fixed decimal places. Other format types behave differently:

  • 'f gives fixed-point decimal output'
  • 'e gives scientific notation'
  • 'g uses a more compact general format'

If you specifically want a fixed number of digits after the decimal point, use f. Do not use g and expect the same behavior, because g may remove trailing zeros.

Common Pitfalls

The most common mistake is expecting round() and formatted output to behave identically in every context. Another is forgetting that floating-point display is still built on binary floating-point values, which can make some rounding results look odd. Developers also use a format type such as g and then wonder why trailing zeros disappear. Finally, some code formats numbers inline everywhere instead of using one small helper, which makes presentation rules harder to keep consistent.

Summary

  • Use f"{value:.2f}" to show exactly two digits after the decimal point.
  • Replace 2 with any other precision you need.
  • Combine precision with width, alignment, and zero padding when formatting tables or reports.
  • Use dynamic precision with f"{value:.{digits}f}" when needed.
  • Prefer Decimal when exact decimal behavior matters more than ordinary float convenience.

Course illustration
Course illustration

All Rights Reserved.