Skip to content

Commit

Permalink
Merge pull request #919 from serde-rs/displaynum
Browse files Browse the repository at this point in the history
Preserve '.0' when Displaying Number
  • Loading branch information
dtolnay authored Aug 21, 2022
2 parents 6b8b073 + cb2515b commit 2c8e2b0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 12 deletions.
15 changes: 3 additions & 12 deletions src/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,9 @@ impl Display for Number {
#[cfg(not(feature = "arbitrary_precision"))]
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
match self.n {
N::PosInt(u) => Display::fmt(&u, formatter),
N::NegInt(i) => Display::fmt(&i, formatter),
N::Float(f) => Display::fmt(&f, formatter),
N::PosInt(u) => formatter.write_str(itoa::Buffer::new().format(u)),
N::NegInt(i) => formatter.write_str(itoa::Buffer::new().format(i)),
N::Float(f) => formatter.write_str(ryu::Buffer::new().format_finite(f)),
}
}

Expand All @@ -305,15 +305,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: 2 additions & 0 deletions tests/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ 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.0"); // not just "1"
assert_eq!(Number::from_f64(12e40).unwrap().to_string(), "1.2e41");
}

#[test]
Expand Down

0 comments on commit 2c8e2b0

Please sign in to comment.