>>> a = "545.2222"
>>> float(a)
545.22220000000004
>>> int(float(a))
545
Answer from Harley Holcombe on Stack Overflow
๐ŸŒ
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 - Learn how to convert strings to floats in Python using float(). Includes syntax, examples, error handling tips, and real-world use cases for data parsing.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ convert-string-to-float-in-python
Convert String to Float in Python - GeeksforGeeks
July 15, 2025 - 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.
Top answer
1 of 16
3117
>>> a = "545.2222"
>>> float(a)
545.22220000000004
>>> int(float(a))
545
2 of 16
605

Python2 method to check if a string is a float:

def is_float(value):
  if value is None:
      return False
  try:
      float(value)
      return True
  except:
      return False

For the Python3 version of is_float see: Checking if a string can be converted to float in Python

A longer and more accurate name for this function could be: is_convertible_to_float(value)

What is, and is not a float in Python may surprise you:

The below unit tests were done using python2. Check it that Python3 has different behavior for what strings are convertable to float. One confounding difference is that any number of interior underscores are now allowed: (float("1_3.4") == float(13.4)) is True

val                   is_float(val) Note
--------------------  ----------   --------------------------------
""                    False        Blank string
"127"                 True         Passed string
True                  True         Pure sweet Truth
"True"                False        Vile contemptible lie
False                 True         So false it becomes true
"123.456"             True         Decimal
"      -127    "      True         Spaces trimmed
"\t\n12\r\n"          True         whitespace ignored
"NaN"                 True         Not a number
"NaNanananaBATMAN"    False        I am Batman
"-iNF"                True         Negative infinity
"123.E4"              True         Exponential notation
".1"                  True         mantissa only
"1_2_3.4"             False        Underscores not allowed
"12 34"               False        Spaces not allowed on interior
"1,234"               False        Commas gtfo
u'\x30'               True         Unicode is fine.
"NULL"                False        Null is not special
0x3fade               True         Hexadecimal
"6e7777777777777"     True         Shrunk to infinity
"1.797693e+308"       True         This is max value
"infinity"            True         Same as inf
"infinityandBEYOND"   False        Extra characters wreck it
"12.34.56"            False        Only one dot allowed
u'ๅ››'                 False        Japanese '4' is not a float.
"#56"                 False        Pound sign
"56%"                 False        Percent of what?
"0E0"                 True         Exponential, move dot 0 places
0**0                  True         0___0  Exponentiation
"-5e-5"               True         Raise to a negative number
"+1e1"                True         Plus is OK with exponent
"+1e1^5"              False        Fancy exponent not interpreted
"+1e1.3"              False        No decimals in exponent
"-+1"                 False        Make up your mind
"(1)"                 False        Parenthesis is bad

You think you know what numbers are? You are not so good as you think! Not big surprise.

Don't use this code on life-critical software!

Catching broad exceptions this way, killing canaries and gobbling the exception creates a tiny chance that a valid float as string will return false. The float(...) line of code can failed for any of a thousand reasons that have nothing to do with the contents of the string. But if you're writing life-critical software in a duck-typing prototype language like Python, then you've got much larger problems.

๐ŸŒ
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.

๐ŸŒ
IONOS
ionos.com โ€บ digital guide โ€บ websites โ€บ web development โ€บ converting python strings to floats
How to convert Python strings to floats - IONOS
January 2, 2025 - In Python, only numerical values can be used in calculations and math operations. We explain how to convert Python strings into floats.
๐ŸŒ
Cherry Servers
cherryservers.com โ€บ home โ€บ blog โ€บ python โ€บ how to convert string to float in python (6 different ways)
How to Convert String to Float in Python (6 Different Ways) | Cherry Servers
July 25, 2024 - ... Using the float() function is the most common way to convert string to float in Python. Whatโ€™s more, it is a built-in function which means it comes with standard Python installation.
๐ŸŒ
Medium
medium.com โ€บ @ryan_forrester_ โ€บ converting-strings-to-floats-in-python-a-complete-guide-f0ec19bf30a0
Converting Strings to Floats in Python: A Complete Guide | by ryan | Medium
October 28, 2024 - The `float()` function handles both regular decimal numbers and scientific notation. Itโ€™s worth noting that Python uses dots (.) rather than commas (,) as decimal separators. Real-world data often comes in various formats. Hereโ€™s how to handle them: # Removing currency symbols price_string = "$99.99" price = float(price_string.replace("$", "")) print(price) # Output: 99.99 # Converting percentage strings percentage = "85.5%" decimal = float(percentage.strip("%")) / 100 print(decimal) # Output: 0.855 # Handling thousand separators large_number = "1,234,567.89" cleaned_number = float(large_number.replace(",", "")) print(cleaned_number) # Output: 1234567.89
Find elsewhere
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_func_float.asp
Python float() Function
Remove List Duplicates Reverse ... Python Bootcamp Python Certificate Python Training ... The float() function converts the specified value into a floating point number. ... If you want to ......
๐ŸŒ
Flexiple
flexiple.com โ€บ python โ€บ string-to-float-python
Convert string to float in Python - Flexiple Tutorials - Flexiple
In this solution, we use the most common method to convert any data type including a string to float in Python; float().
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ examples โ€บ string-to-float-or-int
Python Program to Parse a String to a Float or Int
If the string is a float numeral, you can convert it into a float type using float(), and then parse it to an integer using int().
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Convert a String to a Number (int, float) in Python | note.nkmk.me
April 29, 2025 - In Python, you can convert a string (str) to a number using int() for an integer or float() for a floating-point number. Convert a string to int: int() Convert a string to float: float() Convert a bin ...
๐ŸŒ
Replit
replit.com โ€บ home โ€บ discover โ€บ how to convert a string to a float in python
How to convert a string to a float in Python
The pattern r'\d+\.\d+' tells Python to find sequences of one or more digits (\d+), followed by a literal dot (\.), and then more digits. It returns a list of all matching number strings. You can then convert these to floats using a list comprehension, efficiently pulling numerical data from unstructured text.
๐ŸŒ
Jeremy Morgan
jeremymorgan.com โ€บ python โ€บ how-to-convert-a-string-to-a-float-in-python
How to Convert a String to a Float in Python
In this article, we covered the basics of converting strings to floats in Python, including the float() function and its use cases. We also explored more advanced usage of converting multiple strings or entire files of data into floating-point ...
Top answer
1 of 1
1
data-user_answers_id="5008">How To Convert a String to a Float in PythonIn this article, we'll explore how to convert strings to numbers using Python's float() function. Additionally, we'll learn how to convert numbers back to strings using Python's str() function.It's crucial to convert data types correctly before using them for calculations and combining them to avoid unexpected errors while the program is running.Using the float() function:In Python, you can change a string into a floating-point number using the float() function. This built-in function is designed to convert an object into a floating-point number. Essentially, the float() function invokes the specified object's float() function internally.ExampleLet's check out a simple example of converting a string to a float in Python:string_value = '9.56'float_value = float(string_value)print(type(float_value))print('Float Value =', float_value)OutputFloat Value = 9.56In this example, the float() function is used to convert the string "9.56" to a floating-point number. The resulting value is stored in the variable float_number.Why do we need to convert a string to float?When users provide float values through the terminal or when reading from a file, these values are initially in the form of string objects. To carry out operations like addition, multiplication, and more, it's important to convert these strings into float so that we can perform the required mathematical operations accurately.Examplevalue_1 = input('Please enter the first floating-point value:\n')value_1 = float(value_1)value_2 = input('Please enter the second floating-point value:\n')value_2 = float(value_2)print(f'Sum of {value_1} and {value_2} is {value_1 + value_2}')OutputPlease enter the first floating-point value:23.34Please enter the second floating-point value:365.34Sum of 23.34 and 365.34 is 388.6799The sum of 23.34 and 365.34 is calculated to be 388.6799.Using the str() function:We can also change a float to a string using the str() function. This becomes useful when we need to combine or concatenate float values with other strings.Example:value_1 = 1.23value_2 = 2.34value_3 = 3.45print(f'Concatenation of {value_1} and {value_2} is {str(value_1) + str(value_2)}')print(f'CSV from {value_1}, {value_2}, and {value_3}:\n{str(value_1)},{str(value_2)},{str(value_3)}')print(f'CSV from {value_1}, {value_2}, and {value_3}:\n{", ".join()}')OutputConcatenation of 1.23 and 2.34 is 1.232.34CSV from 1.23, 2.34, and 3.45:1.23,2.34,3.45CSV from 1.23, 2.34, and 3.45:1.23, 2.34, 3.45When we concatenate 1.23 and 2.34, the result is the string '1.232.34'. Additionally, this code generates two versions of comma-separated values (CSV).In the above program, if we skip converting floats to strings, the join() function will cause an exception. Additionally, using the + operator for concatenation won't work correctly, as it would attempt to add the floating-point numbers instead.Conclusion:In this tutorial, we've covered the conversion between strings and numbers in Python using the float() and str() functions. Ensuring proper data type conversion is crucial for accurate and error-free operations in a program.We started by exploring the float() function, which is used to convert strings to floating-point numbers. This is particularly important when handling user input or reading data from files where numeric values are initially in string form. We demonstrated its usage through examples, highlighting the significance of this conversion for mathematical operations.
๐ŸŒ
FavTutor
favtutor.com โ€บ blogs โ€บ string-to-float-python
6 Ways to Convert String to Float in Python | FavTutor
August 31, 2021 - If any number is greater than this, you can indicate them by the string โ€˜infโ€™ in Python ... You can use the float() function to convert any data type into a floating-point number.
๐ŸŒ
Python Engineer
python-engineer.com โ€บ posts โ€บ string-to-float
How to convert a string to float/integer and vice versa in Python - Python Engineer
The result will include the whole number part of the float, as the decimal part is discarded during float to int conversion. ... ```python number_as_string = "3.14159" number_as_float = float(number_as_string) number_as_integer = int(number_as_float) print(number_as_integer)
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ how to convert string to float in python?
Convert String to Float in Python - Scaler Topics
May 5, 2022 - For converting the list of strings ... have to iterate the string list and take the values one by one and then convert the string values to the floating values and then append all the float values to the floating value list...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how i convert float to string in this case?
r/learnpython on Reddit: How I convert float to string in this case?
August 21, 2022 -

I tried

n1=input('First number')
n2=input('Second number')
sum = float(n1) + float(n2)
str(sum)
print('The sum of the values is: ' + sum)

My error is:

TypeError: can only concatenate str (not "float") to str

I tried googling this error and got some answers like print(f' which I didn't really understand, and some others that looked a little complicated, I am very new.

I am trying to improve my googling skills.