Skip to content

Commit 1a2d1fb

Browse files
committed
Pull precision and width from fmt::Formatter and pass on to write!.
Would prefer to pass entire object not just specific attributes, but this works for the common case (i.e. :.x).
1 parent df5aa85 commit 1a2d1fb

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/temperature.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,16 @@ impl ::std::cmp::PartialOrd for TemperatureDelta {
164164

165165
impl ::std::fmt::Display for Temperature {
166166
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
167-
write!(f, "{:.1} \u{00B0}C", self.as_celsius())
167+
let p = f.precision().unwrap_or(1);
168+
let w = f.width().unwrap_or(0);
169+
write!(f, "{temp:width$.prec$}\u{00A0}\u{00B0}C", prec=p, width=w, temp=self.as_celsius())
168170
}
169171
}
170172

171173
impl ::std::fmt::Display for TemperatureDelta {
172174
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
173-
write!(f, "{:.1} C\u{00B0}", self.as_celsius())
175+
let p = f.precision().unwrap_or(1);
176+
let w = f.width().unwrap_or(0);
177+
write!(f, "{temp:width$.prec$}\u{00A0}\u{00B0}C", prec=p, width=w, temp=self.as_celsius())
174178
}
175179
}

0 commit comments

Comments
 (0)