Unfortunately it seems that not even the new-style formatting with float.__format__ supports this. The default formatting of floats is the same as with repr; and with f flag there are 6 fractional digits by default:

>>> format(0.0000000005, 'f')
'0.000000'

However there is a hack to get the desired result - not the fastest one, but relatively simple:

  • first the float is converted to a string using str() or repr()
  • then a new Decimal instance is created from that string.
  • Decimal.__format__ supports f flag which gives the desired result, and, unlike floats it prints the actual precision instead of default precision.

Thus we can make a simple utility function float_to_str:

import decimal

# create a new context for this task
ctx = decimal.Context()

# 20 digits should be enough for everyone :D
ctx.prec = 20

def float_to_str(f):
    """
    Convert the given float to a string,
    without resorting to scientific notation
    """
    d1 = ctx.create_decimal(repr(f))
    return format(d1, 'f')

Care must be taken to not use the global decimal context, so a new context is constructed for this function. This is the fastest way; another way would be to use decimal.local_context but it would be slower, creating a new thread-local context and a context manager for each conversion.

This function now returns the string with all possible digits from mantissa, rounded to the shortest equivalent representation:

>>> float_to_str(0.1)
'0.1'
>>> float_to_str(0.00000005)
'0.00000005'
>>> float_to_str(420000000000000000.0)
'420000000000000000'
>>> float_to_str(0.000000000123123123123123123123)
'0.00000000012312312312312313'

The last result is rounded at the last digit

As @Karin noted, float_to_str(420000000000000000.0) does not strictly match the format expected; it returns 420000000000000000 without trailing .0.

Answer from Antti Haapala on Stack Overflow
Top answer
1 of 7
74

Unfortunately it seems that not even the new-style formatting with float.__format__ supports this. The default formatting of floats is the same as with repr; and with f flag there are 6 fractional digits by default:

>>> format(0.0000000005, 'f')
'0.000000'

However there is a hack to get the desired result - not the fastest one, but relatively simple:

  • first the float is converted to a string using str() or repr()
  • then a new Decimal instance is created from that string.
  • Decimal.__format__ supports f flag which gives the desired result, and, unlike floats it prints the actual precision instead of default precision.

Thus we can make a simple utility function float_to_str:

import decimal

# create a new context for this task
ctx = decimal.Context()

# 20 digits should be enough for everyone :D
ctx.prec = 20

def float_to_str(f):
    """
    Convert the given float to a string,
    without resorting to scientific notation
    """
    d1 = ctx.create_decimal(repr(f))
    return format(d1, 'f')

Care must be taken to not use the global decimal context, so a new context is constructed for this function. This is the fastest way; another way would be to use decimal.local_context but it would be slower, creating a new thread-local context and a context manager for each conversion.

This function now returns the string with all possible digits from mantissa, rounded to the shortest equivalent representation:

>>> float_to_str(0.1)
'0.1'
>>> float_to_str(0.00000005)
'0.00000005'
>>> float_to_str(420000000000000000.0)
'420000000000000000'
>>> float_to_str(0.000000000123123123123123123123)
'0.00000000012312312312312313'

The last result is rounded at the last digit

As @Karin noted, float_to_str(420000000000000000.0) does not strictly match the format expected; it returns 420000000000000000 without trailing .0.

2 of 7
38

If you are satisfied with the precision in scientific notation, then could we just take a simple string manipulation approach? Maybe it's not terribly clever, but it seems to work (passes all of the use cases you've presented), and I think it's fairly understandable:

def float_to_str(f):
    float_string = repr(f)
    if 'e' in float_string:  # detect scientific notation
        digits, exp = float_string.split('e')
        digits = digits.replace('.', '').replace('-', '')
        exp = int(exp)
        zero_padding = '0' * (abs(int(exp)) - 1)  # minus 1 for decimal point in the sci notation
        sign = '-' if f < 0 else ''
        if exp > 0:
            float_string = '{}{}{}.0'.format(sign, digits, zero_padding)
        else:
            float_string = '{}0.{}{}'.format(sign, zero_padding, digits)
    return float_string

n = 0.000000054321654321
assert(float_to_str(n) == '0.000000054321654321')

n = 0.00000005
assert(float_to_str(n) == '0.00000005')

n = 420000000000000000.0
assert(float_to_str(n) == '420000000000000000.0')

n = 4.5678e-5
assert(float_to_str(n) == '0.000045678')

n = 1.1
assert(float_to_str(n) == '1.1')

n = -4.5678e-5
assert(float_to_str(n) == '-0.000045678')

Performance:

I was worried this approach may be too slow, so I ran timeit and compared with the OP's solution of decimal contexts. It appears the string manipulation is actually quite a bit faster. Edit: It appears to only be much faster in Python 2. In Python 3, the results were similar, but with the decimal approach slightly faster.

Result:

  • Python 2: using ctx.create_decimal(): 2.43655490875

  • Python 2: using string manipulation: 0.305557966232

  • Python 3: using ctx.create_decimal(): 0.19519368198234588

  • Python 3: using string manipulation: 0.2661344590014778

Here is the timing code:

from timeit import timeit

CODE_TO_TIME = '''
float_to_str(0.000000054321654321)
float_to_str(0.00000005)
float_to_str(420000000000000000.0)
float_to_str(4.5678e-5)
float_to_str(1.1)
float_to_str(-0.000045678)
'''
SETUP_1 = '''
import decimal

# create a new context for this task
ctx = decimal.Context()

# 20 digits should be enough for everyone :D
ctx.prec = 20

def float_to_str(f):
    """
    Convert the given float to a string,
    without resorting to scientific notation
    """
    d1 = ctx.create_decimal(repr(f))
    return format(d1, 'f')
'''
SETUP_2 = '''
def float_to_str(f):
    float_string = repr(f)
    if 'e' in float_string:  # detect scientific notation
        digits, exp = float_string.split('e')
        digits = digits.replace('.', '').replace('-', '')
        exp = int(exp)
        zero_padding = '0' * (abs(int(exp)) - 1)  # minus 1 for decimal point in the sci notation
        sign = '-' if f < 0 else ''
        if exp > 0:
            float_string = '{}{}{}.0'.format(sign, digits, zero_padding)
        else:
            float_string = '{}0.{}{}'.format(sign, zero_padding, digits)
    return float_string
'''

print(timeit(CODE_TO_TIME, setup=SETUP_1, number=10000))
print(timeit(CODE_TO_TIME, setup=SETUP_2, number=10000))
๐ŸŒ
Finxter
blog.finxter.com โ€บ how-to-print-a-float-without-scientific-notation-in-python
How to Print a Float Without Scientific Notation in Python? โ€“ Be on the Right Side of Change
... The most powerful way is to ... are enclosed in f'...'. Within a given f-string, you can use the {...:f} format specifier to tell Python to use floating point notation for the number preceding the :f suffix....
Discussions

python - Casting float to string without scientific notation - Stack Overflow
The float: fl = 0.000005 casts to String as str(fl)=='5e-06'. however, I want it to cast as str(fl)='0.000005' for exporting to CSV purposes. How do I achieve this? More on stackoverflow.com
๐ŸŒ stackoverflow.com
How do I convert scientific notation number to float?
It is already a float. If you want a str with fixed decimals then you should use float formatting. One way to do this is f"{num:.20f}" More on reddit.com
๐ŸŒ r/learnpython
14
84
March 25, 2022
python - How to suppress scientific notation when printing float values? - Stack Overflow
Here's my code: x = 1.0 y = 100000.0 print x/y My quotient displays as 1.00000e-05. Is there any way to suppress scientific notation and make it display as 0.00001? I'm going to use the resul... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Converting float to string without scientific notation - Stack Overflow
I am working with python / pandas. My dataframe contains one column called 'id' with 20-digit IDs like 1225485903482773506 with datatype float. If I convert the column to string with df['id'] = df[... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
YouTube
youtube.com โ€บ how to fix your computer
PYTHON : Convert float to string in positional format (without scientific notation and false precis - YouTube
PYTHON : Convert float to string in positional format (without scientific notation and false precision) [ Gift : Animated Search Engine : https://www.hows.te...
Published ย  December 8, 2021
Views ย  73
๐ŸŒ
YouTube
youtube.com โ€บ watch
Converting float to string in Python Pandas without Scientific Notation - YouTube
Learn how to convert float values in a dataframe to string format without scientific notation in Python using Pandas.---This video is based on the question h...
Published ย  October 2, 2025
Views ย  3
๐ŸŒ
Python Pool
pythonpool.com โ€บ home โ€บ blog โ€บ python scientific notation with suppressing and conversion
Python Scientific Notation With Suppressing And Conversion - Python Pool
January 1, 2024 - Happy Pythoning! ... Can suppress scientific notation in this value or lower 1e-05. The output shouldnโ€™t be a string. ... Yes, you can use โ€œ{:e}โ€.format() to suppress the numbers. But unfortunately, youโ€™ll have to use string while printing. For declaring, you can use ... This article demonstrates a fundamental misunderstanding of floating points in Python.
Find elsewhere
๐ŸŒ
Finxter
blog.finxter.com โ€บ python-string-to-float-scientific-notation-easy-conversion-guide
Python String to Float Scientific Notation: Easy Conversion Guide โ€“ Be on the Right Side of Change
January 19, 2024 - To format these floats as a string, Python provides the format() function, which allows you to control the precision and appearance of your float: ... In this example, .2f specifies that the float should be converted to a string with two characters after the decimal point. When youโ€™re dealing with scientific notation directly, format_float_scientific from the NumPy library is extremely useful.
๐ŸŒ
DNMTechs
dnmtechs.com โ€บ home โ€บ blog โ€บ converting float to string in python 3: positional format without scientific notation or false precision
Converting Float to String in Python 3: Positional Format without Scientific Notation or False Precision - DNMTechs - Sharing and Storing Technology Knowledge
January 23, 2024 - One common issue is the appearance of scientific notation or false precision in the output. However, by using the positional format specifier, we can overcome these challenges and obtain a string representation of a float without scientific ...
๐ŸŒ
Sage Q&A Forum
ask.sagemath.org โ€บ question โ€บ 32912 โ€บ show-full-non-scientific-notation-of-very-small-decimal
Show full (non-scientific-notation) of very small decimal? - ASKSAGE: Sage Q&A Forum
March 31, 2016 - Combine with print to get those displayed without the surrounding ' quotes. sage: a = 1/2^70 sage: aa = '%.72f' % a sage: print(aa) 0.000000000000000000000847032947254300339068322500679641962051391601562500 sage: ab = '{:.72f}'.format(float(a)) sage: print(ab) 0.000000000000000000000847032947254300339068322500679641962051391601562500
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-suppress-scientific-notation-when-printing-float-values
How to suppress scientific notation when printing float values? - GeeksforGeeks
July 10, 2024 - Suppressing scientific notation when printing float values is essential for improving readability in various applications. By using string formatting, f-strings in Python, or specific library functions like those in NumPy, you can control how float values are displayed, ensuring they meet your formatting preferences without exponential notation.
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ suppressing scientific notation in python for float values
Suppressing Scientific Notation in Python for Float Values - AskPython
April 29, 2023 - The format function in python can be used to suppress the display of scientific notation. The syntax of the function is as shown below: ... For example, if you only need 9 points after the decimal point, it may be written as f'{result:.9f}' ...
๐ŸŒ
Python Reference
python-reference.readthedocs.io โ€บ en โ€บ latest โ€บ docs โ€บ functions โ€บ format.html
format โ€” Python Reference (The Right Way) 0.1 documentation
The precision is a decimal number indicating how many digits should be displayed after the decimal point for a floating point value formatted with โ€˜fโ€™ and โ€˜Fโ€™, or before and after the decimal point for a floating point value formatted with โ€˜gโ€™ or โ€˜Gโ€™. For non-number types the field indicates the maximum field size - in other words, how many characters will be used from the field content. The precision is not allowed for integer values. Determines how the data should be presented. The available string presentation types are: โ€˜sโ€™
๐ŸŒ
Kitchin Research Group
kitchingroup.cheme.cmu.edu โ€บ blog โ€บ 2013 โ€บ 01 โ€บ 21 โ€บ Controlling-the-format-of-printed-variables
Controlling the format of printed variables
You may want to only show a few decimal places, or print in scientific notation, or embed the result in a string. Here are some examples of printing with no control over the format. a = 2./3 print a print 1/3 print 1./3. print 10.1 print "Avogadro's number is ", 6.022e23,'.' 0.666666666667 0 0.333333333333 10.1 Avogadro's number is 6.022e+23 . There is no control over the number of decimals, or spaces around a printed number. In python, we use the format function to control how variables are printed.
๐ŸŒ
Javaer101
javaer101.com โ€บ en โ€บ article โ€บ 917003.html
Convert float to string in positional format (without scientific notation and false precision) - Javaer101
This leads to the question: what ... as in repr(n) (or str(n) on Python 3), but always using the decimal format, not the scientific notation. That is, a function or operation that for example converts the float value 0.00000005 to string '0.00000005'; 0.1 to '0.1'; ...