Since this post might be here for a while, lets also point out python 3 syntax:

"{:.2f}".format(5)
Answer from Daren Thomas on Stack Overflow
๐ŸŒ
Finxter
blog.finxter.com โ€บ python-string-to-float-with-2-decimals-easy-conversion-guide
Python String to Float with 2 Decimals: Easy Conversion Guide โ€“ Be on the Right Side of Change
Hereโ€™s the syntax for a float with two decimal places: your_float = 7.326 formatted_float = f"{your_float:.2f}" print(formatted_float) # Output: 7.33 ยท F-strings are a powerful and friendly way to handle precision and formatting directly within your string literals. In this section, youโ€™ll see how to translate string representations of numbers in Python ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-get-two-decimal-places-in-python
How to get two decimal places in Python - GeeksforGeeks
July 23, 2025 - The f-string in Python is used to format a string. It can also be used to get 2 decimal places of a float value.
๐ŸŒ
freeCodeCamp
forum.freecodecamp.org โ€บ python
Floating point numbers to two decimal places - possible with standard lib? - Python - The freeCodeCamp Forum
August 24, 2022 - Iโ€™m trying to get 2 decimal places of 0s (e.g, 9.00) in the budget app project. I tried using string formatting, but the program expects a floating point number, not a string. Casting it to a float results in a single .0 because thatโ€™s how floating point numbers work in Python - that gets ...
๐ŸŒ
Analytics Vidhya
analyticsvidhya.com โ€บ home โ€บ 6 ways to round floating value to two decimals in python
6 Ways to Round Floating Value to Two Decimals in Python
November 4, 2024 - However, modern formatting methods ... To format a floating-point number with two decimal places in Python, you can use the .2f format specifier....
๐ŸŒ
PythonHow
pythonhow.com โ€บ how โ€บ limit-floats-to-two-decimal-points
Here is how to limit floats to two decimal points in Python
Here is an example of how to use format to limit a float to two decimal points: x = 3.14159265 # Format x as a string with two decimal points y = "{:.2f}".format(x) print(y) # Output: "3.14"The format function takes a format string as the first argument and the value to format as the second argument.
Find elsewhere
๐ŸŒ
ItSolutionstuff
itsolutionstuff.com โ€บ post โ€บ python-convert-string-to-float-with-2-decimal-places-exampleexample.html
Python Convert String to Float with 2 Decimal Places Example - ItSolutionstuff.com
October 30, 2023 - We first convert the string to a float using the float() function, and then round it to 2 decimal places using the round() function. Finally, we print the float_number variable, which will output 4.14.
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ 5 best ways to convert a python string to float with 2 decimals
5 Best Ways to Convert a Python String to Float With 2 Decimals - Be on the Right Side of Change
February 19, 2024 - The Decimal module provides support for fast correctly-rounded decimal floating point arithmetic. We can use this module to round a string to two decimal places accurately. ... from decimal import Decimal, ROUND_HALF_UP num_string = "123.4567" decimal_num = Decimal(num_string).quantize(Decimal('0.00'), rounding=ROUND_HALF_UP)
๐ŸŒ
Python Guides
pythonguides.com โ€บ convert-string-to-float-with-2-decimal-places-in-python
Convert String To Float With 2 Decimal Places In Python
May 16, 2025 - This method works by first converting the string to a float using float(), then rounding to 2 decimal places using round(). Check out How to Add Line Breaks in Python Strings?
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-round-to-two-decimal-places
How to Round to 2 Decimal Places in Python | DataCamp
August 8, 2024 - The % operator offers a traditional method of formatting numbers to two decimal places. The % operator allows for creating formatted strings by inserting values into placeholders. In the example below, f indicates the value as a floating-point number while .2 specifies the decimal places to ...
Top answer
1 of 16
2330

You are running into the old problem with floating point numbers that not all numbers can be represented exactly. The command line is just showing you the full floating point form from memory.

With floating point representation, your rounded version is the same number. Since computers are binary, they store floating point numbers as an integer and then divide it by a power of two so 13.95 will be represented in a similar fashion to 125650429603636838/(2**53).

Double precision numbers have 53 bits (16 digits) of precision and regular floats have 24 bits (8 digits) of precision. The floating point type in Python uses double precision to store the values.

For example,

>>> 125650429603636838/(2**53)
13.949999999999999

>>> 234042163/(2**24)
13.949999988079071

>>> a = 13.946
>>> print(a)
13.946
>>> print("%.2f" % a)
13.95
>>> round(a,2)
13.949999999999999
>>> print("%.2f" % round(a, 2))
13.95
>>> print("{:.2f}".format(a))
13.95
>>> print("{:.2f}".format(round(a, 2)))
13.95
>>> print("{:.15f}".format(round(a, 2)))
13.949999999999999

If you are after only two decimal places (to display a currency value, for example), then you have a couple of better choices:

  1. Use integers and store values in cents, not dollars and then divide by 100 to convert to dollars.
  2. Or use a fixed point number like decimal.
2 of 16
838

There are new format specifications, String Format Specification Mini-Language:

You can do the same as:

"{:.2f}".format(13.949999999999999)

Note 1: the above returns a string. In order to get as float, simply wrap with float(...):

float("{:.2f}".format(13.949999999999999))

Note 2: wrapping with float() doesn't change anything:

>>> x = 13.949999999999999999
>>> x
13.95
>>> g = float("{:.2f}".format(x))
>>> g
13.95
>>> x == g
True
>>> h = round(x, 2)
>>> h
13.95
>>> x == h
True
๐ŸŒ
Python Guides
pythonguides.com โ€บ python-print-2-decimal-places
How to Print Two Decimal Places in Python
December 22, 2025 - # Monthly subscription cost for ... the :.2f is the magic part. The colon starts the format specifier, the .2 indicates two decimal places, and the f tells Python to treat the value as a float....
๐ŸŒ
Real Python
realpython.com โ€บ how-to-python-f-string-format-float
How to Format Floats Within F-Strings in Python โ€“ Real Python
April 24, 2024 - You achieved this by adding the format specifier .2f into the replacement field. The 2 is the precision, while the lowercase f is an example of a presentation type. Youโ€™ll see more of these later.
๐ŸŒ
Finxter
blog.finxter.com โ€บ python-convert-float-to-string
Python Convert Float to String โ€“ Be on the Right Side of Change
my_float = 999999999999999999.999999999999 my_string = f'{my_float:.2E}' print(my_string) # 1.00E+18 ยท Thanks for your attention! ๐Ÿ™‚ If you want to keep learning, feel free to join our email academy (we have cheat sheets!): A similar problem is converting a float list to a string list in Python.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ python limit floats to two decimal points
Python Limit Floats to Two Decimal Points - Spark By {Examples}
May 31, 2024 - # Quick examples of limiting floats to two decimal points # Example 1: Using the round() function rounded_num = round(num, 2) print(rounded_num) # Example 2: Using string formatting rounded_num = "{:.2f}".format(num) print(rounded_num) # Example ...
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 3288381 โ€บ how-to-round-to-2-decimal-places-in-python
How to round to 2 decimal places in python | Sololearn: Learn to code for FREE!
In python there is no such function. to get it done, we can use an f-string like this: val = 42.1 print(f'{val:.2f}') # for more details have a look at the python docs result is: 42.10 An answer proposed by Lothar
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 49429798 โ€บ float-string-number-to-float-with-2-decimals
python - Float string number to float with 2 decimals? - Stack Overflow
def float_(): price_data = {} str_num = '3.505,32' price_data['price'] = float(str_num.replace('.','').replace(',','.')) price_data['currency'] = 'USD' return price_data
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ python string to float conversion
r/learnpython on Reddit: Python string to float conversion
July 29, 2024 -

Hi all, I have a string a = '1721244344.700249000', I want to convert it to a floating value.

Float() is returning only 2 places after decimal point. Like 1721244344.7

Is there a way I can convert the entire string to a floating point value and get all the decimal (upto 9 places after decimal point)?

I have to use python v2.7 for this.

Edit: I do not have problem in printing the all 9 decimal places but I need the floating value so that I can subtract another value so that I get the difference with accuracy upto 9 th decimal point.