>>> a = "545.2222"
>>> float(a)
545.22220000000004
>>> int(float(a))
545
Answer from Harley Holcombe on Stack Overflow
Top answer
1 of 16
3118
>>> 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.

🌐
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
Discussions

How I convert float to string in this case?
You shouldn't call your variable sum, as that's a name of a built in function: https://docs.python.org/3/library/functions.html#sum Your problem however, stems from trying to add a string to a float. Could you please tell me, what is Adam + 5? Well, you can't, because it makes no mathematical sense. You didn't save the string representation str(sum), so sum never changed to a string What your research found is f-strings and they are very easy to use. Try: print(f"the sum of the values is {sum}") Simply, put an f before a string starts, then any string that you want goes between " and ", while any variables or other values go between { and } More on reddit.com
🌐 r/learnpython
4
2
August 21, 2022
Python - decimal places (putting floats into a string) - Stack Overflow
where "0" refers to the first value passed into str.format, ":" says "here comes the format specification", and ".2f" means "floating point number with two decimal places of precision". This is the suggested way of formatting strings now. ... For Python version 3.6 and upwards there is also ... More on stackoverflow.com
🌐 stackoverflow.com
Python string to float conversion
I don't know what you mean: >>> a = '1721244344.700249000' >>> float(a) 1721244344.700249 These are all the decimal places. Trailing zeros will always be omitted as they are irrelevant. If you want to show them, do so when you output the value: >>> print(a.format("{:.9}")) 1721244344.700249000 More on reddit.com
🌐 r/learnpython
18
5
July 29, 2024
Converting strings into floats in Python

Try float(nameOfString), I'm not a python guy but it looks like that would work. Are you using Python3?

More on reddit.com
🌐 r/learnprogramming
13
1
November 6, 2015
🌐
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.

🌐
GeeksforGeeks
geeksforgeeks.org › python › cannot-convert-string-to-float-in-python
Cannot Convert String To Float in Python - GeeksforGeeks
July 23, 2025 - Below, code replaces the comma in the string "1,23456" with a dot, creating "1.23456". Subsequently, it converts the modified string to a float, resulting in the value 1.23456. This transformation allows the string to be compatible with the float data type in Python.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-string-float-to-float-list-in-python
Convert String Float to Float List in Python - GeeksforGeeks
July 12, 2025 - For example, s = '1.23 4.56 7.89' ... 4.56, 7.89]. By using split() on a string containing float numbers, we can break it into individual string elements and then apply map() to convert each element into a float....
🌐
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
February 5, 2026 - 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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-string-to-float-in-python
Convert String to Float in Python - GeeksforGeeks
July 15, 2025 - float() function is an efficient way to convert a string into a floating-point number. As a built-in function, it returns a value of type float and works best with well-formatted numeric strings.
🌐
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.

🌐
W3Schools
w3schools.com › python › python_string_formatting.asp
Python String Formatting
To format values in an f-string, add placeholders {}, a placeholder can contain variables, operations, functions, and modifiers to format the value. ... A placeholder can also include a modifier to format the value.
🌐
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 programs that work with various kinds of data, float() is used to ensure that data remains con­sis­tent by con­vert­ing values into a numerical format. To convert Python strings to floats, use float() and enter a valid string as the argument:
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-list-of-float-to-string-conversion
Python - List of float to string conversion - GeeksforGeeks
July 12, 2025 - List comprehension is a Pythonic way to iterate over the list and convert each element to a string. ... List comprehension iterates through the list a and applies str(i) to each element. The result is a new list b where all float elements are converted to strings.
🌐
W3Schools
w3schools.com › python › python_casting.asp
Python Casting
Python is an object-orientated language, and as such it uses classes to define data types, including its primitive types. Casting in python is therefore done using constructor functions: int() - constructs an integer number from an integer literal, a float literal (by removing all decimals), or a string literal (providing the string represents a whole number)
🌐
Board Infinity
boardinfinity.com › blog › converting-string-to-float-into-python
Convert String to Float in Python | Board Infinity
July 11, 2023 - In this solution, we use the most common method to convert any data type including a string to float in Python; 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 - 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.
🌐
Python documentation
docs.python.org › 3 › tutorial › inputoutput.html
7. Input and Output — Python 3.14.3 documentation
The string module contains support for a simple templating approach based upon regular expressions, via string.Template. This offers yet another way to substitute values into strings, using placeholders like $x and replacing them with values from a dictionary.
🌐
Pierian Training
pieriantraining.com › home › tutorial: how to convert string to float in python
Convert String to Float in Python: A Beginner's Guide
April 27, 2023 - Discover how to use Python to convert a string into float in a few easy steps. Learn the syntax, detailed examples, and how to handle errors.
🌐
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
December 9, 2023 - 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 ...
🌐
Python documentation
docs.python.org › 3 › library › stdtypes.html
Built-in Types — Python 3.14.3 documentation
3 weeks ago - Note that the exponent is written in decimal rather than hexadecimal, and that it gives the power of 2 by which to multiply the coefficient. For example, the hexadecimal string 0x3.a7p10 represents the floating-point number (3 + 10./16 + 7./16**2) * 2.0**10, or 3740.0: