From 8ba854166d12e1813aaf09501aa0dd87f3250aed Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 21 Aug 2022 13:38:31 -0700 Subject: [PATCH] Preserve '.0' when Displaying Number --- src/number.rs | 12 ++---------- tests/debug.rs | 2 +- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/src/number.rs b/src/number.rs index 4ee052690..9c1acd1a8 100644 --- a/src/number.rs +++ b/src/number.rs @@ -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) } diff --git a/tests/debug.rs b/tests/debug.rs index 432a1378b..0535bfdd1 100644 --- a/tests/debug.rs +++ b/tests/debug.rs @@ -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]