What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Convert the numeric string with float(), then format the result with .2f when you need exactly two digits after the decimal point:

text = "12.5"
value = float(text)
formatted = f"{value:.2f}"

print(formatted)  # 12.50

value is a float; formatted is a string. The format specifier controls how the number is written, including trailing zeroes.

Convert a string and format it to two decimal places

Use an f-string with the .2f format specifier after converting the string:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
amount_text = "19.995"
amount = float(amount_text)
print(f"{amount:.2f}")

For fixed-point formatting, the number after the dot is the count of digits after the decimal point. Thus .2f produces exactly two fractional digits. Depending on the value and float representation, formatting may round the displayed result.

You can use the built-in format() function for the same output:

formatted = format(float("19.995"), ".2f")

Python’s format specification documentation describes f precision as the number of digits after the decimal point.

Keep the number and its formatted text distinct

Conversion and formatting do different jobs. float(text) constructs a floating-point value for numeric operations; formatting produces text suitable for a label, report, or display.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
value = float("7.4")
label = f"{value:.2f}"  # "7.40"

print(value + 1)  # numeric calculation
print(label)      # text

Keep value for calculations and create label when you need the two-place representation. Formatting does not turn the float into a decimal type or constrain later arithmetic to two decimal places. See Python’s documentation for the float() built-in.

Handle invalid or non-finite input

If a string may not contain a supported number, float() raises ValueError. Catch it where your program can respond appropriately—for example, by showing an error or asking for a corrected value:

def parse_float(text):
    try:
        return float(text)
    except ValueError:
        return None

value = parse_float(user_input)
if value is not None:
    print(f"{value:.2f}")
else:
    print("Enter a valid number.")

float() accepts decimal strings with an optional sign and surrounding whitespace, as well as exponent notation such as "1e-003". It also accepts spellings for NaN and infinity. If your application requires ordinary finite numbers, check that separately:

import math

value = float(user_input)
if math.isfinite(value):
    print(f"{value:.2f}")
else:
    print("Enter a finite number.")

Successful conversion alone does not check application-specific limits or rules.

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

Choose Decimal for decimal-sensitive arithmetic

For a display label, formatting a float with .2f is usually the direct solution. If the calculation itself must use decimal fixed-point arithmetic, use Decimal and explicitly quantize to two decimal places:

from decimal import Decimal

amount = Decimal("19.995")
rounded = amount.quantize(Decimal("0.01"))
print(f"{rounded:.2f}")

Constructing Decimal from the original text avoids first introducing a binary float approximation. The result of quantize() depends on the active Decimal context and its rounding mode, so choose a mode that fits your application. Python documents this fixed-point approach in its Decimal documentation; it does not establish a particular financial or accounting rule.

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

Do not confuse decimal places with significant digits

Use .2f for exactly two digits after the decimal point. .2g instead requests two significant digits and may omit trailing zeroes, so it does not guarantee a two-place result. Likewise, round(value, 2) returns a numeric result but does not by itself guarantee that its displayed text includes two digits after the decimal point.

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.