Skip to content

Instantly share code, notes, and snippets.

@c2t-r
Last active February 15, 2024 06:26
Show Gist options
  • Save c2t-r/c0f9810f802e086c540d7536693b5e0c to your computer and use it in GitHub Desktop.
Save c2t-r/c0f9810f802e086c540d7536693b5e0c to your computer and use it in GitHub Desktop.
文字列に含まれる指数表記を全て小数表記に置き換える
import re

def unexponential(text: str):
    pe = r'((\d+\.\d+|\d+)e(\+|\-)[1-9]+\d*)'
    found = re.findall(pe, text)
    for i in found:
        base = i[0]
        fvalue = str(float(base))
        if "+" in i: fvalue = fvalue[0:-2]
        text = text.replace(base, fvalue)
    return text
    
print(unexponential("11e+1"))

正規表現は好きだから使っただけなので効率は良くないと思います。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment