Skip to content

Commit

Permalink
Make Display for Number produce the same representation as serializing
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Aug 21, 2022
1 parent 8ba8541 commit cb2515b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +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),
// Preserve `.0` on integral values, which Display hides
N::Float(f) => Debug::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 Down
1 change: 1 addition & 0 deletions tests/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ fn value_number() {
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 cb2515b

Please sign in to comment.