Convert the string with float(), then format the number with .2f when you need text with 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 formatting adds a trailing zero for display without changing the type or arithmetic precision of the float.
As an Amazon Associate I earn from qualifying purchases.
Convert a string and format it to two decimal places
Use Python’s built-in float() to convert valid numeric text, then use an f-string with :.2f to produce a fixed-point string with exactly two digits after the decimal point:
Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Clear out junk files and repair common Windows errorsFree Scan →amount_text = "19.995"
amount = float(amount_text)
print(f"{amount:.2f}")
The output is 20.00. In the format specifier, .2 requests two digits of precision and f selects fixed-point formatting. Python documents f precision as the number of digits following the decimal point. Python format specification
#1 Best Overall
You can use the format() built-in for the same result:
formatted = format(float("19.995"), ".2f")
Keep the number separate from its formatted text
Formatting creates text; it does not turn the value into a decimal type or limit the float’s underlying arithmetic to two places. Keep the float for calculations and format it at the point where you display or emit a label:
Rank #2
value = float("7.4")
label = f"{value:.2f}" # "7.40"
print(value + 1) # numeric calculation
print(label) # text for display
Use .2f when the requirement is a two-place string. round(value, 2) rounds a numeric value, but it does not guarantee trailing zeroes in its displayed representation.
Handle invalid or non-finite input
Unsupported text raises ValueError. If the input may be invalid, catch the error and choose a response that fits your program, such as asking the user to try again or returning a validation result:
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}")
float() accepts decimal text with an optional sign and surrounding whitespace, as well as exponent notation such as "1e-003". It also accepts spellings of NaN and infinity, so successful conversion alone does not establish that a value is finite or within your application’s permitted range. If finite numbers are required, check with math.isfinite() after conversion. Python built-in functions
Use Decimal for decimal-sensitive arithmetic
For money or other calculations where decimal representation and a defined rounding policy matter, use Decimal rather than relying on a float’s display formatting. Construct it from the original string, then use quantize(Decimal("0.01")) to express a two-place quantum:
from decimal import Decimal
amount = Decimal("19.995")
rounded = amount.quantize(Decimal("0.01"))
print(f"{rounded:.2f}")
The rounding result depends on the active Decimal context and its rounding mode; select one that matches your application’s rules. This is an arithmetic choice, unlike formatting a float merely to show two digits. Python decimal documentation
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 →Why .2g does not mean two decimal places
In the format mini-language, f precision counts digits after the decimal point, while g precision counts significant digits and may omit trailing zeroes. Use .2f for two digits after the decimal point; .2g has a different meaning. Python format specification
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.




