>>> a = "545.2222"
>>> float(a)
545.22220000000004
>>> int(float(a))
545
Answer from Harley Holcombe on Stack Overflow
Top answer
1 of 16
3120
>>> a = "545.2222"
>>> float(a)
545.22220000000004
>>> int(float(a))
545
2 of 16
606

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.

๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ python โ€บ how do i parse a string to a float or int?
How do I parse a string to a float or int? | Sentry
January 30, 2023 - For example, I have a string like โ€œ1337โ€ or โ€œ13.37โ€ and I want to parse it to an int or float data type. If your string input is clean and predictable, the easiest way to parse a string to a float or int is by using the float or int built-in modules.
๐ŸŒ
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.

๐ŸŒ
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 - By the end, youโ€™ll understand the best practices for handling any string-to-float conversion task. Pythonโ€™s built-in float() function handles most string-to-float conversions, including integers, decimals, negative numbers, scientific notation, and strings with leading/trailing whitespace.
๐ŸŒ
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.
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ python string to float, float to string
Python String to float, float to String - AskPython
February 16, 2023 - Python provides us with the built-in float() method to convert the data type of input from String to float.
Find elsewhere
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-convert-float-to-string-in-python
How to convert float to string in Python
On the other hand, in languages like JavaScript or Python, there is no need to explicitly mention a variableโ€™s datatype at the time of declaration as the datatype is set according to the value assigned to it. #JavaScript var my_int = 123 var my_float = 123.123 var my_string = "123" #Python my_int = 123 my_float = 123.123 my_string = "123"
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ how to convert string to float in python?
Convert String to Float in Python - Scaler Topics
May 5, 2022 - When working with terminal or file operations in Python programming, such as reading or writing a file, the user input is a string object. Although you requested a floating-point value, Python's input function returns the user input as a string object. As a result, you must explicitly convert the string to a floating-point value in order to do the required operations.
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ python โ€บ python float to string
How to Convert Float to String in Python | Delft Stack
February 25, 2025 - This tutorial provides a comprehensive guide on how to convert a float to a string in Python. Learn various methods including str(), format(), and f-strings, complete with well-commented code examples. Perfect for beginners and seasoned developers alike, this article offers step-by-step ...
๐ŸŒ
Quora
quora.com โ€บ How-can-I-convert-a-float-to-a-string-in-Python
How to convert a float to a string in Python - Quora
Answer (1 of 7): # First take any variable and assign any value to it float = 1.2 # Next take another variable(result in this code) # and use python str keyword to convert float value # to str and assign that value to another variable(result in this code) result = str(float) # print the seco...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ convert-string-to-float-in-python
Convert String to Float in Python - GeeksforGeeks
July 15, 2025 - ast.literal_eval() function safely evaluates strings that contain Python literals, such as numbers or lists. It can securely convert a numeric string to a float.
๐ŸŒ
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 ...
๐ŸŒ
Centron
centron.de โ€บ startseite โ€บ how to convert a string to a float in python
How to Convert a String to a Float in Python
February 7, 2025 - And we will also use Pythonโ€™s str() function to convert floats to strings.
๐ŸŒ
Finxter
blog.finxter.com โ€บ python-convert-float-to-string
Python Convert Float to String โ€“ Be on the Right Side of Change
March 9, 2024 - If you use the f'{my_float:.2f}' expression, Python converts the float to a string representation of the rounded float with n digits.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ python convert string to float
Python Convert String to Float - Spark By {Examples}
May 21, 2024 - A float is a numerical value with a decimal point, represented as a float object in Python. Representation: A string is represented by enclosing a sequence of characters in single or double quotes, like this: 'Hello, World!' or "3.14159". A float is represented by a numerical value with a decimal point, like this: 3.14159. Usage: Strings are typically used to represent text data, such as names, addresses, or messages.
๐ŸŒ
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.
๐ŸŒ
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.