Skip to content

Commit 50d0b93

Browse files
committed
Fix also g/mol and g/l
1 parent a0dc65d commit 50d0b93

1 file changed

Lines changed: 13 additions & 12 deletions

File tree

si-units/src/lib.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,19 @@ impl PySIObject {
8080
if let Ok(v) = self.value.extract::<f64>(py) {
8181
Ok(SINumber::new(v, self.unit).to_string())
8282
} else {
83-
let value = self
84-
.value
85-
.call_method0(py, "__repr__")?
86-
.extract::<String>(py)?;
87-
let unit = if self.unit == _KILOGRAM {
88-
// because the base unit already has a prefix, we cannot call
89-
// unit.to_string() on it (it would return 'g').
90-
"kg".into()
91-
} else {
92-
self.unit.to_string()
93-
};
94-
Ok(format!("{} {}", value, unit))
83+
let mut value = self.value.bind(py).clone();
84+
if self.unit == _KILOGRAM
85+
|| self.unit == _KILOGRAM / _MOL
86+
|| self.unit == _KILOGRAM / _METER.powi(3)
87+
{
88+
// For these three specific units, unit.to_string() will not return the
89+
// base unit (kg...) but rather g, in order to determine an appropriate
90+
// prefix for scalars. In this general representation, there are no prefixes
91+
// so we adjust the value to match the unit before printing.
92+
value = self.value.bind(py).mul(1e3)?;
93+
}
94+
let value = value.call_method0("__repr__")?.extract::<String>()?;
95+
Ok(format!("{} {}", value, self.unit))
9596
}
9697
}
9798

0 commit comments

Comments
 (0)