Skip to content

Commit a4e9267

Browse files
__format__ should fall back to str when spec='' (#63)
2 parents ab19d82 + b683154 commit a4e9267

File tree

2 files changed

+4
-0
lines changed

2 files changed

+4
-0
lines changed

test/test_value.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,6 @@ def test_format() -> None:
286286
x = 42.235 * tu.GHz
287287
assert f'{x:0.2f}' == '42.23 GHz'
288288
assert f'{x:0.1e}' == '4.2e+01 GHz'
289+
290+
assert f'{tu.GHz}' == 'GHz'
291+
assert f'{2*tu.GHz}' == '2 GHz'

tunits/core/cython/with_unit.pyx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,7 @@ cdef class WithUnit:
729729
self.base_units.__setstate__(pickle_info['base_units'])
730730

731731
def __format__(self, spec: str) -> str:
732+
if spec == '' and self.value == 1: return str(self)
732733
return self.value.__format__(spec) + ' ' + str(self.unit)
733734

734735
_try_interpret_as_with_unit = None

0 commit comments

Comments
 (0)