Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
10 additions
and
3 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |