Konvertieren Sie den exponentiellen Float in Python in str

Um den numerischen Typ in Python in den Zeichenfolgentyp zu ändern, können Sie ihn mit str () umwandeln. Wenn Sie jedoch den Float des Exponentialausdrucks mit str () umwandeln, ist das Ergebnis etwas uncool.

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'

Zu diesem Zeitpunkt str (b) '0.00000000000000000000000000000000000000001' Ich möchte, dass du wirst.

Ich konnte es mit den unten beschriebenen Funktionen gut machen.

[Referenz] 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

Konvertieren Sie den exponentiellen Float in Python in str
Konvertieren Sie die CIDR-Notation in Python
Konvertieren Sie die Netzmaske der CIDR-Notation in Python in eine gepunktete Dezimalschreibweise
Konvertieren Sie die psd-Datei in Python in png
In Python von Markdown in HTML konvertieren
Konvertieren Sie die absolute URL in eine relative URL in Python
Konvertieren Sie die FBX-Datei in Python in ASCII <-> BINARY
Konvertieren Sie PDFs mit Python in Massenbilder
Klassennotation in Python
Konvertieren Sie kubischen Netzcode in Python in WKT
Konvertieren Sie Datum und Uhrzeit in Zeitzonen in Unixtime in Python2.7
So konvertieren / wiederherstellen Sie einen String mit [] in Python
Konvertieren Sie das NumPy-Array "ndarray" in Python [tolist ()]
So konvertieren Sie Gleitkommazahlen in Python in Binärzahlen
Konvertieren Sie das Bild in .zip mit Python in PDF
Konvertieren Sie die asynchrone API im Callback-Stil in async / await in Python
Konvertieren / Zurückgeben von Klassenobjekten in das JSON-Format in Python
[Python] Erstellt eine Methode zum Konvertieren von Radix in 1 Sekunde
Konvertieren Sie den Webpay-Entitätstyp in den Dict-Typ (rekursiv in Python).
[Python] Datum in Zeichenfolge konvertieren
So löschen Sie stdout in Python
Konvertieren Sie numpy int64 in python int
[Python] Liste in Pandas konvertieren [Pandas]
Melden Sie sich auf der Website in Python an
Konvertieren Sie das Scratch-Projekt in Python
[Python] Konvertieren Sie Shift_JIS in UTF-8
Sprechen mit Python [Text zu Sprache]
Wie man in Python entwickelt
Konvertieren Sie Python 3.x-Code in Python 2.x.
Post an Slack in Python
[Python] Konvertiert die Zeitanzeige (str-Typ) mit "" "und" '"in Sekunden (float-Typ) mit datetime und timedelta
So konvertieren Sie den Python # -Typ für Python-Superanfänger: int, float
Konvertieren Sie das an Jason Stay Samlike in Python übergebene Bild in ASCII Art
Konvertieren Sie eine Excel-Datei für verschiedene Zwecke in Python in Text
[Python] Wie man PCA mit Python macht
Inklusive Notation im Argument der Python-Funktion
So sammeln Sie Bilder in Python
Verwendung von SQLite in Python
Workflow zum Konvertieren der Formel (Bild) in Python
Im Python-Befehl zeigt Python auf Python3.8
Konvertieren Sie die Liste mit Python in DataFrame
Versuchen Sie, Trace in Python zu berechnen
So konvertieren Sie 0,5 in 1056964608 auf einmal
Python> Liste> Doppelte Liste in einfache Liste konvertieren
Wie man MySQL mit Python benutzt
So verpacken Sie C in Python
Verwendung von ChemSpider in Python
6 Möglichkeiten zum Stringen von Objekten in Python
Verwendung von PubChem mit Python
[Python] Konvertiert natürliche Zahlen in Ordnungszahlen
Konvertieren Sie die Dezimalzahl in n-ary [Python]
Umgang mit Japanisch mit Python
Python> Tupel> Konvertiert doppeltes Tupel in einzelnes Tupel
Eine Alternative zu "Pause" in Python
Bestimmen Sie das Datums- und Uhrzeitformat mit Python und konvertieren Sie es in Unixtime
Konvertieren Sie in Python usw. geschriebene Dateien in PDF mit Syntax-Hervorhebung
Lesen Sie die Big-Endian-Binärdatei in Python und konvertieren Sie sie in ndarray
[Python] Der von Openpyxl geschriebene Wert wird als Exponent (E) angezeigt.
Anzeigen von LaTeX-Notationsformeln in Python, matplotlib