Preserve '.0' when Displaying Number

This commit is contained in:
David Tolnay 2022-08-21 13:38:31 -07:00
parent de251c8198
commit 8ba854166d
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
2 changed files with 3 additions and 11 deletions

View File

@ -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),
}
}
@ -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)
}

View File

@ -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]