>>> 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.

🌐
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 - try: not_a_float = float("123.456.789") ... to float") ... Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski. SEE EPISODES ... David Y. — April 15, 2023 ... David Y. — August 15, 2024 ... David Y. — August 15, 2023 · How do I remove a trailing newline from a string in Python...
Discussions

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
October 15, 2018
What is a float in python?
You mean variable? Like integer, boolean, char or string? These are fairly universal in all programming languages. It's a method of storing a number that has decimal points, like for example 2.8, or 13.13245235, or 3.0. You couldn't store them as integer, even "3.0" wouldn't have the same properties, as it wouldn't be divisible by "2". You could possibly store it as an array of chars, or a string, but that's text. If you try to add something to text, it would add character (2.13+16=2.1316) and wouldn't perform mathematical operation. More on reddit.com
🌐 r/explainlikeimfive
3
1
December 22, 2012
Stuck Converting String Variables with $ to Floats?
Never use floats for adding currency. See https://en.wikipedia.org/wiki/Kahan_summation_algorithm and http://www.drdobbs.com/floating-point-summation/184403224 for an explanation. More on reddit.com
🌐 r/Python
6
2
May 2, 2018
🌐
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.
🌐
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.

🌐
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...
🌐
IONOS
ionos.com › digital guide › websites › web development › converting python strings to floats
How to convert Python strings to floats - IONOS
January 2, 2025 - That means de­vel­op­ers working ... to catch and react to errors is one way to do this. The float() function is a built-in Python method that converts strings to floats....
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-string-with-comma-to-float-in-python
Convert String with Comma To Float in Python - GeeksforGeeks
July 23, 2025 - In this example, below Python code converts the numeric string "1,234.56" to a float by removing the comma and then prints the result.
🌐
Plain English
python.plainenglish.io › python-type-casting-made-easy-convert-int-str-float-more-aeba2142df41
Python Type Casting Made Easy — Convert int, str, float & More | by Rabail Zaheer | Python in Plain English
June 22, 2025 - Maybe turn a string "123" into a number you can calculate with? Or a number into text to display in a sentence? That’s exactly what this blog is gonna be all about. I am going to walk you through how to convert between data types in Python using super simple real-world examples.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python convert string to float
Python Convert String to Float - Spark By {Examples}
May 21, 2024 - It will correctly convert the string to the corresponding floating-point number. Are there any limitations to converting large numbers to floats? There are limitations due to the finite precision of floating-point representation in computers. Extremely large or small numbers may lose precision. For high-precision arithmetic, consider using libraries like decimal in Python.
🌐
Quora
quora.com › How-do-I-convert-input-strings-to-float-in-Python
How to convert input strings to float() in Python - Quora
There can try builtins or third party library to do this. ... I am not considering how you get data to convert to. You can do that based on your context. Hope this helps. ... Hi you can convert the strings to float using float() .
🌐
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().
🌐
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.
🌐
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.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Convert a String to a Number (int, float) in Python | note.nkmk.me
April 29, 2025 - Built-in Functions - float() — Python 3.13.3 documentation · print(float('1.23')) print(type(float('1.23'))) # 1.23 # <class 'float'> ... Strings that contain only a fractional part or represent a whole number can also be converted using float(). print(float('.23')) # 0.23 print(float('100')) print(type(float('100'))) # 100.0 # <class 'float'> ... By specifying the base as the second argument to int(), you can convert strings in binary, octal, or hexadecimal notation to integers.
🌐
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
🌐
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.
🌐
PythonHow
pythonhow.com › how › parse-a-string-to-a-float-or-integer
Here is how to parse a string to a float or integer in Python
To parse a string to a float or int in Python, you can use the float() and int() methods, respectively. These methods take a string as their argument and attempt to convert it to a float or int, respectively. If the conversion is successful, they return the converted value; otherwise, they ...
🌐
Caasify
caasify.com › home › blog › master python string to float conversion: handle errors, locale, and formats
Master Python String to Float Conversion: Handle Errors, Locale, and Formats | Caasify
October 6, 2025 - The solution is a built-in Python function called float(), which converts a string representing a number into a floating-point number. This allows for mathematical operations with the number, such as addition, subtraction, or multiplication.
🌐
Python documentation
docs.python.org › 3 › library › functions.html
Built-in Functions — Python 3.14.3 documentation
February 27, 2026 - The string must not contain whitespace between '+', '-', the 'j' or 'J' suffix, and the decimal number. For example, complex('1+2j') is fine, but complex('1 + 2j') raises ValueError. More precisely, the input must conform to the complexvalue production rule in the following grammar, after parentheses and leading and trailing whitespace characters are removed: complexvalue: floatvalue | floatvalue ("j" | "J") | floatvalue sign absfloatvalue ("j" | "J")
🌐
Intellipaat
intellipaat.com › home › blog › how to parse a string to a float or int in python?
How to parse a string to a float or int in Python? - Intellipaat
February 3, 2026 - Note: Using strip() datatype we can parse the string to int by removing the spaces that are present in the string. ... Code Copied! ... Using the Python built-in datatype float() we convert the string to float.