With Python < 3 (e.g. 2.6 [see comments] or 2.7), there are two ways to do so.

# Option one
older_method_string = "%.9f" % numvar

# Option two
newer_method_string = "{:.9f}".format(numvar)

But note that for Python versions above 3 (e.g. 3.2 or 3.3), option two is preferred.

For more information on option two, I suggest this link on string formatting from the Python documentation.

And for more information on option one, this link will suffice and has info on the various flags.

Python 3.6 (officially released in December of 2016), added the f string literal, see more information here, which extends the str.format method (use of curly braces such that f"{numvar:.9f}" solves the original problem), that is,

# Option 3 (versions 3.6 and higher)
newest_method_string = f"{numvar:.9f}"

solves the problem. Check out @Or-Duan's answer for more info, but this method is fast.

Answer from jyalim on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › convert float to string without losing precision.
r/learnpython on Reddit: Convert float to string without losing precision.
February 23, 2021 -

I am looking to manipulate a data frame of floats which all need 6 decimal points after manipulation.

I am looking to add brackets and () around the floats based on conditionals which is why I need to convert to strings. I then can concat the two strings together

However when I convert to str, it reduces the number of decimals to 2.

For example

-35.920000 Original Dataframe

Converted to str

-35.92 After conversion

If I convert the string back to a float, it does not retain the 6 decimals from the original df.

My understanding is both values are stored the same and they both are logically = when checked in the notebook , but for management reasons I am trying to see if there is a way to coerce the string method the take a literal copy of the float, rather than reducing it down.

Sorry for the formatting, I am on mobile .

Thanks

🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to convert python float to string with precision
5 Best Ways to Convert Python Float to String with Precision - Be on the Right Side of Change
February 16, 2024 - This code snippet converts a float, pi, into a string with four decimal places of precision using str.format(). It outputs the string “3.1416”. String interpolation with f-strings, available from Python 3.6, is a readable way to embed expressions inside string literals.
People also ask

How to format floating numbers using inbuilt methods in Python?
We can use the 'format' method to specify the width and assignment of the floating numbers.
🌐
askpython.com
askpython.com › home › python floating point formatting: 2 simple methods
Python Floating Point Formatting: 2 Simple Methods - AskPython
How do I limit a float to only show 2 decimal places?
You can limit a float to 2 decimal places using either f-strings or the format() method. For f-strings, use f"{value:.2f}" and for format(), use "{:.2f}".format(value).
🌐
askpython.com
askpython.com › home › python floating point formatting: 2 simple methods
Python Floating Point Formatting: 2 Simple Methods - AskPython
How do I display infinity or nan values properly?
Use the .2g format to display inf, -inf, and nan in a consistent readable format like you would expect in a calculator.
🌐
askpython.com
askpython.com › home › python floating point formatting: 2 simple methods
Python Floating Point Formatting: 2 Simple Methods - AskPython
🌐
Real Python
realpython.com › how-to-python-f-string-format-float
How to Format Floats Within F-Strings in Python – Real Python
April 24, 2024 - To use Python’s format specifiers in a replacement field, you separate them from the expression with a colon (:). As you can see, your float has been rounded to two decimal places. You achieved this by adding the format specifier .2f into the replacement field. The 2 is the precision, while the lowercase f is an example of a presentation type. You’ll see more of these later. Note: When you use a format specifier, you don’t actually change the underlying number. You only improve its display. Python’s f-strings also have their own mini-language that allows you to format your output in a variety of different ways.
Top answer
1 of 2
19

but should I really force manually how many decimal numbers I want? Yes.

And even with specifying 10 decimal digits, you are still not printing all of them. Floating point numbers don't have that kind of precision anyway, they are mostly approximations of decimal numbers (they are really binary fractions added up). Try this:

>>> format(38.2551994324, '.32f')
'38.25519943239999776096738060005009'

there are many more decimals there that you didn't even specify.

When formatting a floating point number (be it with '%f' % number, '{:f}'.format(number) or format(number, 'f')), a default number of decimal places is displayed. This is no different from when using str() (or '%s' % number, '{}'.format(number) or format(number), which essentially use str() under the hood), only the number of decimals included by default differs; Python versions prior to 3.2 use 12 digits for the whole number when using str().

If you expect your rational number calculations to work with a specific, precise number of digits, then don't use floating point numbers. Use the decimal.Decimal type instead:

  • Decimal “is based on a floating-point model which was designed with people in mind, and necessarily has a paramount guiding principle – computers must provide an arithmetic that works in the same way as the arithmetic that people learn at school.” – excerpt from the decimal arithmetic specification.

  • Decimal numbers can be represented exactly. In contrast, numbers like 1.1 and 2.2 do not have exact representations in binary floating point. End users typically would not expect 1.1 + 2.2 to display as 3.3000000000000003 as it does with binary floating point.

2 of 2
3

I would use the modern str.format() method:

>>> '{}'.format(38.2551994324)
'38.2551994324'

The modulo method for string formatting is now deprecated as per PEP-3101

🌐
AskPython
askpython.com › home › python floating point formatting: 2 simple methods
Python Floating Point Formatting: 2 Simple Methods - AskPython
April 10, 2025 - To format floats, include the desired precision in curly brackets after the f: ... In this example, the :.2f inside the curly braces states that the floating-point number should be formatted with two decimal places.
🌐
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.

🌐
EyeHunts
tutorial.eyehunts.com › home › python string format float precision | example code
Python string format float precision | Example code
April 22, 2022 - The precision is a value that goes right after the dot character. ... The above returns a string. In order to get as float, simply wrap with float(...): A simple example code prints a formatted floating-point value. x = 13.949999999999999999 ...
Find elsewhere
🌐
Finxter
blog.finxter.com › python-convert-float-to-string
Python Convert Float to String – Be on the Right Side of Change
March 9, 2024 - The most Pythonic way to convert a float to a string is to pass the float into the built-in str() function. For example, str(1.23) converts the float 1.23 to the string '1.23'. ... To set the decimal precision after the comma, you can use the f-string formatting functionality in Python 3.
🌐
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.
🌐
Finxter
blog.finxter.com › 5-best-ways-to-convert-python-string-to-float-with-precision
5 Best Ways to Convert Python String to Float with Precision – Be on the Right Side of Change
The round() function rounds the float to 6 decimal places, the precision of the original string. This method allows you to specify the number of decimal places but can also round the number, potentially losing some precision. Using Python’s formatted string literals (f-strings) or the format() ...
🌐
Python documentation
docs.python.org › 3 › tutorial › floatingpoint.html
15. Floating-Point Arithmetic: Issues and Limitations — Python 3.14.3 documentation
While pathological cases do exist, ... decimal digits you expect. str() usually suffices, and for finer control see the str.format() method’s format specifiers in Format string syntax....
🌐
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 - The important thing about the Decimal() function is that it’s more precise compared to the built-in float() function, which can sometimes introduce a small error due to the binary floating-point representation.
🌐
py4u
py4u.org › blog › python-precision-in-string-formatting-with-float-numbers
Python Float String Formatting: How to Preserve Full Precision Without Manual Decimal Specification
When you convert a float to a string, ... returns '0.1'). To "preserve full precision," we need a string that captures the exact binary value of the float, without truncation or unnecessary trailing zeros....
Top answer
1 of 16
2331

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
🌐
Python
docs.python.org › 3 › library › string.html
Common string operations — Python 3.14.3 documentation
The available presentation types for float and Decimal values are: The result should be correctly rounded to a given precision p of digits after the decimal point. The rounding mode for float matches that of the round() builtin.
🌐
Python
docs.python.org › 3 › library › decimal.html
decimal — Decimal fixed-point and floating-point arithmetic
If the argument is a string, no leading or trailing whitespace or underscores are permitted. ... Creates a new Decimal instance from a float f but rounding using self as the context. Unlike the Decimal.from_float() class method, the context ...
🌐
Medium
medium.com › @coucoucamille › float-formatting-in-python-ccb023b86417
Simple Float Formatting in Python | by Coucou Camille | Medium
June 15, 2022 - Python’s built-in format() function allows you to format float in any way you prefer. Syntax: {:.2f}.format(num) for rounding to 2 decimal places. {} marks a replacement field · : introduces a format specifier · .2 specify the precision as 2, or any other number ·