Skip to content

Python script for engineering format

[cce_python]
# -*- coding: cp1252 -*-
from math import tan, pi, sqrt, log, floor

def eng(x, sigfigs=3):
m = floor(log(x)/log(10)/3)*3
LUT = {
-18: “a”,
-15: “f”,
-12: “p”,
-9: “n”,
-6: “µ”,
-3: “m”,
0: “”,
3: “k”,
6: “M”,
9: “G”
}
return (“{0:.{1}g}{2}”).format(x*10.0**(-m), sigfigs, LUT[m])
[/cce_python]

I’m not sure what grief the µ character is going to cause people. Feel free to change it to “u”.

Be the first to like.

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*