Skip to content

Instantly share code, notes, and snippets.

@andre-hartmann
Last active July 4, 2023 06:26
Show Gist options
  • Save andre-hartmann/52490bfad2a51fba1faa6b653e40abe2 to your computer and use it in GitHub Desktop.
Save andre-hartmann/52490bfad2a51fba1faa6b653e40abe2 to your computer and use it in GitHub Desktop.
# $HOME/.local/lib/python3.10/site-packages/engineer.py
# Usage: `python3 -i -c "from engineer import *"`
# >>> eng(1/48e6)
# '20.833E-9'
# Stellt die zahl im Ingenieursformat 123E-6 oder 234E3 dar
def eng(zahl):
# Sonderfall: Zahl ist 0
if zahl == 0:
return "0.0"
# Ermitteln des Exponenten im Ingenieursformat
exponent = 0
while abs(zahl) < 1:
zahl *= 1000
exponent -= 3
while abs(zahl) >= 1000:
zahl /= 1000
exponent += 3
# Bestimmen des Exponenten-Strings
exponent_string = ""
if exponent != 0:
exponent_string = "E{}".format(exponent)
# Formatierung der Zahl im Ingenieursformat
return "{:.3f}{}".format(zahl, exponent_string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment