meas: render the SI prefix

This commit is contained in:
2022-07-30 21:15:51 -07:00
parent 542d700f69
commit 530ab890e6

View File

@@ -74,13 +74,12 @@ impl Measurement {
pub fn str_value(&self) -> String {
use MeasurementValue::*;
let val = match self.value {
Field(v) => v.to_string(),
Float(f) => f.to_string(),
Int(u) => u.to_string(),
Dim(v) => format!("{}x{}x{}", v.x(), v.y(), v.z()),
};
format!("{}{}", val, self.unit)
match self.value {
Field(v) => format!("{}{}", v, self.unit),
Float(f) => SiScale::format_short(f, &self.unit),
Int(u) => format!("{}{}", u, self.unit),
Dim(v) => format!("{}x{}x{}{}", v.x(), v.y(), v.z(), self.unit),
}
}
}
@@ -161,7 +160,7 @@ impl SiScale {
/// e.g. `format_short(1234, "A") -> "1.23 kA"
fn format_short(v: f32, unit: &str) -> String {
let si = SiScale::for_value(v);
let scaled = si.scale() * v;
let scaled = v / si.scale();
format!("{:.2} {}{}", scaled, si.shortcode(), unit)
}
}