Skip to content

Commit f09c86a

Browse files
committed
Can format Lengths.
1 parent 1a2d1fb commit f09c86a

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

examples/format_test.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
extern crate measurements;
22
use measurements::Temperature;
3+
use measurements::Length;
34
fn main() {
45
let t = Temperature::from_celsius(0.1);
6+
let d = Length::from_meters(10000.1);
57
println!("{0:.9} outside", t);
6-
println!("{0:.9} outside", t.as_celsius());
8+
println!("{0:.9} C outside", t.as_celsius());
9+
println!("{0:.3} away", d);
10+
println!("{0:.3} m away", d.as_meters());
711
}

src/length.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ implement_measurement! { Length }
157157

158158
impl ::std::fmt::Display for Length {
159159
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
160-
write!(f, "{:.1} m", self.as_meters())
160+
let p = f.precision().unwrap_or(1);
161+
let w = f.width().unwrap_or(0);
162+
write!(f, "{value:width$.prec$}\u{00A0}m", prec=p, width=w, value=self.as_meters())
161163
}
162164
}

0 commit comments

Comments
 (0)