Skip to content

Commit

Permalink
Preserve '.0' when Displaying Number
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Aug 21, 2022
1 parent de251c8 commit 8ba8541
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 11 deletions.
12 changes: 2 additions & 10 deletions src/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@ impl Display for Number {
match self.n {
N::PosInt(u) => Display::fmt(&u, formatter),
N::NegInt(i) => Display::fmt(&i, formatter),
N::Float(f) => Display::fmt(&f, formatter),
// Preserve `.0` on integral values, which Display hides
N::Float(f) => Debug::fmt(&f, formatter),
}
}

Expand All @@ -305,15 +306,6 @@ impl Display for Number {
}

impl Debug for Number {
#[cfg(not(feature = "arbitrary_precision"))]
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
match self.n {
N::PosInt(_) | N::NegInt(_) => write!(formatter, "Number({})", self),
N::Float(f) => write!(formatter, "Number({:?})", f),
}
}

#[cfg(feature = "arbitrary_precision")]
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "Number({})", self)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn value_number() {
assert_eq!(format!("{:?}", json!(1)), "Number(1)");
assert_eq!(format!("{:?}", json!(-1)), "Number(-1)");
assert_eq!(format!("{:?}", json!(1.0)), "Number(1.0)");
assert_eq!(Number::from_f64(1.0).unwrap().to_string(), "1");
assert_eq!(Number::from_f64(1.0).unwrap().to_string(), "1.0"); // not just "1"
}

#[test]
Expand Down

0 comments on commit 8ba8541

Please sign in to comment.