Last active
July 4, 2023 06:26
-
-
Save andre-hartmann/52490bfad2a51fba1faa6b653e40abe2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# $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