You are confusing float values with their string representation. float(3) is enough, and whenever you need to print one, use formatting.

va = float('3')
print format(va, '.2f')
print isinstance(va, float)

float objects themselves have no concept of a number of decimal places to track.

Answer from Martijn Pieters 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
In Python, the process of converting a string to a float involves using the float() function. This function takes a string as input and converts it to a floating point number, which is a number with a decimal point. Hereโ€™s how you can convert a string into a float: your_string = "123.456" ...
๐ŸŒ
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 - numberStr = "4.14159265359" # Convert number into float with 2 decimal floatNumber = round(float(numberStr), 2) print(floatNumber) ... In this example, we start with the number 4.14159265359.
๐ŸŒ
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.

๐ŸŒ
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 - Read How to Remove Prefixes from Strings in Python? When converting strings to floats, especially from user input or external data sources, errors can occur if the string isnโ€™t a valid number. def safe_float_convert(value_str, decimal_places=2): try: return round(float(value_str), decimal_places) except ValueError: print(f"Error: '{value_str}' cannot be converted to a float") return None # Examples print(safe_float_convert("45.67")) # Output: 45.67 print(safe_float_convert("$45.67")) # Error message and None
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ convert-string-to-float-in-python
Convert String to Float in Python - GeeksforGeeks
July 15, 2025 - It converts a string into a Decimal object, effectively avoiding common floating-point rounding errors. ... eval() function reads and runs a string as a Python expression. It can turn a numeric string like "33.28" into a float. However, it's risky to use with untrusted input because it can run harmful code.
Find elsewhere
๐ŸŒ
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 - This snippet first converts the string to a float, formats it to have two decimal places using the string format operator โ€˜%โ€™, and then converts the formatted string back to a float, resulting in final_num. The format() function in Python is used to format a value into a specific format.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ convert string to decimal in python
Convert String to Decimal in Python - Spark By {Examples}
May 21, 2024 - You can use string formatting to control the precision of a floating-point number when converting a string to a decimal using the float() function. For example, the "{:.2f}".format(decimal_number) syntax is used for string formatting.
๐ŸŒ
Finxter
blog.finxter.com โ€บ python-convert-float-to-string
Python Convert Float to String โ€“ Be on the Right Side of Change
Note that the float value 1.23456789 is rounded to 1.235 with three digits after the decimal place. To set the precision after the comma when converting a float to a string in Python, you can also use the string format() method. For example, the expression '{:.2f}'.format(x) converts the float ...
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-convert-string-to-float
How to Convert String to Float in Python: Complete Guide with Examples | DigitalOcean
July 10, 2025 - Itโ€™s a two-step process: you first convert the string to a float, then format that float to two decimal places. For Display (Get a String): Use an f-string with 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
The format string specifies the desired formatting for the value, and the {:.2f} syntax specifies that the value should be formatted as a float with two decimal points. Both round and format can be used to limit floats to a fixed number of decimal points in Python.
๐ŸŒ
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....
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
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-round-floating-value-to-two-decimals-in-python
How to Round Floating Value to Two Decimals in Python - GeeksforGeeks
July 23, 2025 - We can use string formatting with % operator to format a float value to two decimal places.Here we are using string formatting with % operator to round the given value up to two decimals.
๐ŸŒ
Siluganda
market.siluganda.com โ€บ okts โ€บ python-convert-string-to-float-with-2-decimal-places.html
python convert string to float with 2 decimal places
Using String's format () method. Python format number with commas and round to 2 decimal places. Otherwise the string is normal scientific notation, which is much closer to how the float is internally represented. Idiom #23 Convert real number to string with 2 decimal places.
๐ŸŒ
FavTutor
favtutor.com โ€บ blogs โ€บ string-to-float-python
6 Ways to Convert String to Float in Python | FavTutor
August 31, 2021 - If you want to convert a string into float using NumPy, the first step will be to import the NumPy library using โ€œimport NumPy as np.โ€ Later, you can use the โ€œastypeโ€ method, an in-build method used to convert the string to a floating-point ...
๐ŸŒ
Studytonight
studytonight.com โ€บ python-howtos โ€บ how-to-round-floating-value-to-two-decimals-in-python
How to Round Floating Value to Two Decimals in Python - Studytonight
We can use str.format() function to display float value with two decimal places. It keeps the actual value intact and is simple to implement. ... Sir, your shopping bill amount is Rs.