Convert exponential notation float to str in Python

In Python, to change the numeric type to a string type, you can cast it with str (), but casting the exponential notation float with str () gave somehow awkward results.

Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 0.1
>>> str(a)
'0.1'
>>> b = 0.00000000000000000000000000000000000000001
>>> str(b)
'1e-41'

At this time, str (b) '0.00000000000000000000000000000000000000001' I want you to become.

I was able to do it well by using the functions described below.

[Reference] https://stackoverflow.com/questions/38847690/convert-float-to-string-in-positional-format-without-scientific-notation-and-fa/38983595#38983595

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
Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 0.1
>>> str(a)
'0.1'
>>> b = 0.00000000000000000000000000000000000000001
>>> str(b)
'1e-41'
>>> 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
...
>>> float_to_str(b)
'0.00000000000000000000000000000000000000001'

Recommended Posts

Convert exponential notation float to str in Python
Convert CIDR notation in Python
Convert CIDR notation netmask to dotted decimal notation in Python
Convert psd file to png in Python
Convert from Markdown to HTML in Python
Convert absolute URLs to relative URLs in Python
Convert FBX files to ASCII <-> BINARY in Python
Convert PDFs to images in bulk with Python
Class notation in Python
Convert cubic mesh code to WKT in Python
Convert timezoned date and time to Unixtime in Python2.7
How to convert / restore a string with [] in python
[python] Method to darken RGB value in hexadecimal notation
Convert NumPy array "ndarray" to lilt in Python [tolist ()]
How to convert floating point numbers to binary numbers in Python
Convert the image in .zip to PDF with Python
Convert callback-style asynchronous API to async / await in Python
Convert / return class object to JSON format in Python
[Python] Created a method to convert radix in 1 second
Convert Webpay Entity type to Dict type (recursively in Python)
[python] Convert date to string
To flush stdout in Python
Convert numpy int64 to python int
[Python] Convert list to Pandas [Pandas]
Login to website in Python
Convert Scratch project to Python
[Python] Convert Shift_JIS to UTF-8
Speech to speech in python [text to speech]
How to develop in Python
Convert python 3.x code to python 2.x
Post to Slack in Python
[Python] Convert time display (str type) using "" "and"'" to seconds (float type) with datetime and timedelta
How to generate exponential pulse time series data in python
How to convert Python # type for Python super beginners: int, float
Convert images passed to Jason Statham-like in Python to ASCII art
Convert Excel file to text in Python for diff purposes
[Python] How to do PCA in Python
Included notation in Python function arguments
How to collect images in Python
How to use SQLite in Python
Workflow to convert formula (image) to python
In the python command python points to python3.8
Convert list to DataFrame with python
Try to calculate Trace in Python
How to convert 0.5 to 1056964608 in one shot
Python> list> Convert double list to single list
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
6 ways to string objects in Python
How to use PubChem in Python
[Python] Convert natural numbers to ordinal numbers
Convert decimal numbers to n-ary numbers [python]
How to handle Japanese in Python
Python> tuple> Convert double tuple to single tuple
An alternative to `pause` in Python
Determine the date and time format in Python and convert to Unixtime
Convert files written in python etc. to pdf with syntax highlighting
Read big endian binary in Python and convert it to ndarray
[Python] The value written in Openpyxl is displayed in exponential notation (E).
Display LaTeX notation formulas in Python, matplotlib