How to Convert String to Float with 2 Decimal Places in Python

The quickest way to convert string to float with 2 decimal places in Python is round(float(text), 2). float() parses the text and round() keeps two decimal places. If you need the result as text with both digits, use an f-string instead:

round(float("19.987"), 2)   # 19.99
f"{float('4.50'):.2f}"       # '4.50'

Every output below comes from my own Command Prompt runs on Python 3.12.5 and pandas 3.0.6, using prices and order totals as sample strings.

In this tutorial, we’ll cover round() versus f-strings, exact cents with Decimal, cleaning dollar signs and commas, and whole pandas columns.

Convert a string to a float with 2 decimal places using round()

Wrap float() in round() and you get a real number you can keep calculating with. The catch is the trailing zero: a float never stores it, so 4.50 prints as 4.5:

price_text = "19.987"

price = round(float(price_text), 2)
print(price, type(price).__name__)

print(round(float("4.50"), 2))        # a float drops the trailing zero
print(f"{float('4.50'):.2f}")         # a string keeps it
print(float(f"{float('7.3456'):.2f}"))  # format, then back to float

Output:

19.99 float
4.5
4.50
7.35
Command Prompt output of Python round(float(text), 2) converting price strings to floats with 2 decimal places
"19.987" becomes the float 19.99, while only the f-string keeps 4.50 with both digits.

So ask yourself what you need. For math, keep the float. For a receipt or a report, format it at the end; my guide to printing 2 decimal places in Python goes deeper into display formatting.

Why does round(float(“2.675”), 2) give 2.67?

Floats are binary, so 2.675 is really stored as 2.67499999… and rounds down. Build a Decimal straight from the string and it keeps the exact digits you typed:

from decimal import Decimal, ROUND_HALF_UP

text = "2.675"
print("round(float):", round(float(text), 2))

cents = Decimal("0.01")
print("Decimal:     ", Decimal(text).quantize(cents, rounding=ROUND_HALF_UP))
print("from float:  ", Decimal(float(text)))
print("as float:    ", float(Decimal(text).quantize(cents, rounding=ROUND_HALF_UP)))

Output:

round(float): 2.67
Decimal:      2.68
from float:   2.67499999999999982236431605997495353221893310546875
as float:     2.68
Command Prompt output comparing Python round on a float with Decimal quantize on the string 2.675
The float route returns 2.67, while Decimal("2.675").quantize() with ROUND_HALF_UP returns 2.68.

Notice the third line: Decimal(float(text)) shows the binary error, so always pass the string. I use this for money. How floating point numbers work in Python explains the storage side.

Convert currency strings like “$1,234.567” to a float

Real data rarely arrives clean. Prices come with dollar signs, thousands separators and the odd N/A. Strip the symbols, then convert inside try so bad values don’t crash the loop:

def to_money(text):
    """Turn '$1,234.567' style text into a float with 2 decimals, or None."""
    cleaned = text.strip().replace("$", "").replace(",", "")
    try:
        return round(float(cleaned), 2)
    except ValueError:
        return None

for raw in ["$1,234.567", " 89.9 ", "$0.005", "N/A", "12,50"]:
    print(f"{raw!r:>13} -> {to_money(raw)}")

Output:

 '$1,234.567' -> 1234.57
     ' 89.9 ' -> 89.9
     '$0.005' -> 0.01
        'N/A' -> None
      '12,50' -> 1250.0
Command Prompt output of a Python function converting dollar strings with commas to floats with 2 decimals and returning None for bad text
Dollar amounts come back rounded to cents, and N/A returns None instead of raising an error.

Watch "12,50": removing the comma turns a European 12.50 into 1250.0, so know your source format. The error you’d get without try is covered in fixing could not convert string to float, and comma rules in formatting numbers with commas in Python.

How do I convert a pandas column of strings to 2 decimal places?

For a whole column, skip the loop. Remove the symbols with str.replace(), convert with pd.to_numeric(errors="coerce") and finish with .round(2):

import pandas as pd

orders = pd.DataFrame({"customer": ["Emily", "James", "Olivia", "Noah"],
                       "total": ["$1,049.995", "$87.5", "$12.349", "pending"]})

cleaned = orders["total"].str.replace(r"[$,]", "", regex=True)
orders["amount"] = pd.to_numeric(cleaned, errors="coerce").round(2)

print(orders)
print(orders["amount"].map("{:.2f}".format).tolist())

Output:

  customer       total   amount
0    Emily  $1,049.995  1049.99
1    James       $87.5    87.50
2   Olivia     $12.349    12.35
3     Noah     pending      NaN
['1049.99', '87.50', '12.35', 'nan']
Command Prompt output of pandas converting a column of dollar strings to floats rounded to 2 decimal places
The amount column holds floats rounded to cents, and the text pending becomes NaN.

Look at Emily’s row: $1,049.995 came out as 1049.99, the same binary effect as 2.675. If half-cents matter, use Decimal on the cleaned strings instead.

The last line turns the numbers back into two-decimal strings for export, and the missing value prints as 'nan'. For plain number strings without symbols, converting a string to a float in Python has the basics.

String to 2-decimal methods compared

GoalCodeResult type
Number for mathround(float(s), 2)float
Text with two digitsf"{float(s):.2f}"str
Exact centsDecimal(s).quantize(Decimal("0.01"))Decimal
pandas columnpd.to_numeric(col).round(2)float64

If you’re still working with decimals, these are good next reads:

Frequently asked questions

How do I convert a string to a float with 2 decimal places in Python?

Use round(float(s), 2). For example, round(float("19.987"), 2) returns 19.99.

Why does my float show 4.5 instead of 4.50?

Floats don’t store trailing zeros. Format it for display with f"{x:.2f}", which returns the string '4.50'.

How do I convert a string to a decimal in Python?

Import Decimal from the decimal module and pass the string, for example Decimal("2.675"). Add .quantize(Decimal("0.01")) for two places.

How do I convert a float to a string with 2 decimals?

Use f"{x:.2f}" or format(x, ".2f"). Both round to two places and keep trailing zeros.

How do I convert “$1,234.56” to a float?

Remove the symbols first: float(s.replace("$", "").replace(",", "")) returns 1234.56.

Where is Decimal.quantize() documented?

See Decimal.quantize() in the Python docs.