What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
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:
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minutePC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11amount_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.
#1 Best Overall
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.
Rank #2
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.
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.
Recommended Free Tools
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:
Best Value
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.
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.
Quick Recap
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.

