File tree Expand file tree Collapse file tree 2 files changed +20
-0
lines changed
Expand file tree Collapse file tree 2 files changed +20
-0
lines changed Original file line number Diff line number Diff line change 77
88from sasdata .quantities import units
99from sasdata .quantities .numerical_encoding import numerical_decode , numerical_encode
10+ from sasdata .quantities .unit_parser import parse_unit
1011from sasdata .quantities .units import NamedUnit , Unit
1112
1213T = TypeVar ("T" )
@@ -1180,6 +1181,16 @@ def in_si_with_standard_error(self):
11801181 else :
11811182 return self .in_si (), None
11821183
1184+ def explicitly_formatted (self , unit_string : str ) -> str :
1185+ """Returns quantity as a string with specific unit formatting
1186+
1187+ Performs any necessary unit conversions, but maintains the exact unit
1188+ formatting provided by the user. This can be useful if you have a
1189+ power expressed in horsepower and you want it expressed as "745.7 N m/s" and not as "745.7 W". """
1190+ unit = parse_unit (unit_string )
1191+ quantity = self .in_units_of (unit )
1192+ return f"{ quantity } { unit_string } "
1193+
11831194 def __eq__ (self : Self , other : Self ) -> bool | np .ndarray :
11841195 return self .value == other .in_units_of (self .units )
11851196
@@ -1337,6 +1348,7 @@ def __pow__(self: Self, other: int | float):
13371348
13381349 @staticmethod
13391350 def _array_repr_format (arr : np .ndarray ):
1351+
13401352 """ Format the array """
13411353 order = len (arr .shape )
13421354 reshaped = arr .reshape (- 1 )
Original file line number Diff line number Diff line change @@ -131,3 +131,11 @@ def test_equality():
131131 assert Quantity (1.0 , units .angstroms ) == Quantity (0.1 , units .nanometers )
132132 assert Quantity (1.0 , units .angstroms ) != Quantity (0.1 , units .angstroms )
133133 assert Quantity (1.0 , units .angstroms ) == Quantity (1.0e-10 , units .meters )
134+
135+ @pytest .mark .quantity
136+ def test_explicit_format ():
137+ value = Quantity (1.0 , units .electronvolts )
138+ assert value .explicitly_formatted ("J" ) == "1.602176634e-19 J"
139+ assert value .explicitly_formatted ("N m" ) == "1.602176634e-19 N m"
140+ assert value .explicitly_formatted ("m N" ) == "1.602176634e-19 m N"
141+ assert value .explicitly_formatted ("m kilogram m / hour / year" ) == "1.8201532008477443e-08 m kilogram m / hour / year"
You can’t perform that action at this time.
0 commit comments