Convertir un float exponentiel en str en Python

En Python, pour changer le type numérique en type chaîne, vous pouvez le convertir avec str (), mais lorsque vous transtypez le flottant de l'expression exponentielle avec str (), le résultat n'est pas cool.

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'

À ce moment, str (b) '0.00000000000000000000000000000000000000001' Je veux que tu deviennes.

J'ai pu bien le faire en utilisant les fonctions décrites ci-dessous.

[Référence] 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

Convertir un float exponentiel en str en Python
Convertir la notation CIDR en Python
Convertir le masque de réseau de notation CIDR en notation décimale à points en Python
Convertir un fichier psd en png en Python
Convertir de Markdown en HTML en Python
Convertir une URL absolue en URL relative en Python
Convertir le fichier FBX en ASCII <-> BINARY en Python
Convertissez des PDF en images en masse avec Python
Notation de classe en Python
Convertir le code de maillage cubique en WKT en Python
Convertir la date et l'heure zonées en temps Unixtime dans Python2.7
Comment convertir / restaurer une chaîne avec [] en python
Convertir le tableau NumPy "ndarray" en lilt en Python [tolist ()]
Comment convertir des nombres à virgule flottante en nombres binaires en Python
Convertissez l'image au format .zip en PDF avec Python
Convertir l'API asynchrone de style callback en async / await en Python
Convertir / renvoyer des objets de classe au format JSON en Python
[Python] Création d'une méthode pour convertir la base en 1 seconde
Convertir le type d'entité Webpay en type Dict (récursivement en Python)
[python] Convertir la date en chaîne
Pour vider stdout en Python
Convertir numpy int64 en python int
[Python] Convertir la liste en Pandas [Pandas]
Connectez-vous au site Web en Python
Convertir le projet Scratch en Python
[Python] Convertir Shift_JIS en UTF-8
Parler avec Python [synthèse vocale]
Comment développer en Python
Convertir le code python 3.x en python 2.x
Publier sur Slack en Python
[Python] Convertit l'affichage de l'heure (type str) en utilisant "" "et" '"en secondes (type float) avec datetime et timedelta
Comment convertir le type Python # pour les super débutants de Python: int, float
Convertir l'image passée à Jason Stay Samlike en Python en ASCII Art
Convertir un fichier Excel en texte en Python à des fins de comparaison
[Python] Comment faire PCA avec Python
Notation inclusive dans l'argument de la fonction Python
Comment collecter des images en Python
Comment utiliser SQLite en Python
Workflow pour convertir une formule (image) en python
Dans la commande python, python pointe vers python3.8
Convertir la liste en DataFrame avec python
Essayez de calculer Trace en Python
Comment convertir 0,5 en 1056964608 en un seul coup
Python> liste> Convertir une double liste en une seule liste
Comment utiliser Mysql avec python
Comment envelopper C en Python
Comment utiliser ChemSpider en Python
6 façons d'enchaîner des objets en Python
Comment utiliser PubChem avec Python
[Python] Convertit les nombres naturels en nombres ordinaux
Convertir décimal en n-aire [python]
Comment gérer le japonais avec Python
Python> tuple> Convertir un double tuple en un seul tuple
Une alternative à `pause` en Python
Déterminez le format de la date et de l'heure avec Python et convertissez-le en Unixtime
Convertir des fichiers écrits en python, etc. en pdf avec la coloration syntaxique
Lisez le binaire big endian en Python et convertissez-le en ndarray
[Python] La valeur écrite par Openpyxl est affichée sous forme d'exposant (E).
Afficher les formules de notation LaTeX en Python, matplotlib