Skip to content

Commit

Permalink
Add __format__ method to functional units
Browse files Browse the repository at this point in the history
  • Loading branch information
olebole committed Feb 16, 2023
1 parent 48bc134 commit 0f76ee8
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions astropy/units/function/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,13 @@ def to_string(self, format="generic"):
self_str = "\n".join(lines)
return self_str

def __format__(self, format_spec):
"""Try to format units using a formatter."""
try:
return self.to_string(format=format_spec)
except ValueError:
return format(str(self), format_spec)

def __str__(self):
"""Return string representation for unit."""
self_str = str(self.function_unit)
Expand Down

0 comments on commit 0f76ee8

Please sign in to comment.