diff --git a/gpkitmodels/tools/qstr.py b/gpkitmodels/tools/qstr.py new file mode 100644 index 00000000..b0f14d81 --- /dev/null +++ b/gpkitmodels/tools/qstr.py @@ -0,0 +1,12 @@ +from gpkit.small_scripts import mag + +def qstr(qty, fmtstr="%-.4g"): + 'Converts a number/pint quantity to a nice string for display' + if hasattr(qty, "magnitude"): + magstr = fmt % qty.magnitude + unitstr = u" {:~}".format(qty.units) + if "dimensionless" in unitstr: # boring and long way to put it + unitstr = "" + return magstr + unitstr + else: + return fmt % qty # it's just a number