Skip to content

Commit

Permalink
Update qstr.py
Browse files Browse the repository at this point in the history
  • Loading branch information
bqpd committed Dec 7, 2017
1 parent 4056fbc commit 2a6c079
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions gpkitmodels/tools/qstr.py
@@ -1,5 +1,12 @@
from gpkit.small_scripts import mag

def qstr(qty):
'Converts a pint quantity to a nice string for display'
return "%-.4g%s" % (qty.magnitude, u" {:~}".format(qty.units)) if hasattr(qty, "magnitude") else (qty, "")
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

0 comments on commit 2a6c079

Please sign in to comment.