Skip to content

Commit cb2515b

Browse files
committed
Make Display for Number produce the same representation as serializing
1 parent 8ba8541 commit cb2515b

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

src/number.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,9 @@ impl Display for Number {
292292
#[cfg(not(feature = "arbitrary_precision"))]
293293
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
294294
match self.n {
295-
N::PosInt(u) => Display::fmt(&u, formatter),
296-
N::NegInt(i) => Display::fmt(&i, formatter),
297-
// Preserve `.0` on integral values, which Display hides
298-
N::Float(f) => Debug::fmt(&f, formatter),
295+
N::PosInt(u) => formatter.write_str(itoa::Buffer::new().format(u)),
296+
N::NegInt(i) => formatter.write_str(itoa::Buffer::new().format(i)),
297+
N::Float(f) => formatter.write_str(ryu::Buffer::new().format_finite(f)),
299298
}
300299
}
301300

tests/debug.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ fn value_number() {
2828
assert_eq!(format!("{:?}", json!(-1)), "Number(-1)");
2929
assert_eq!(format!("{:?}", json!(1.0)), "Number(1.0)");
3030
assert_eq!(Number::from_f64(1.0).unwrap().to_string(), "1.0"); // not just "1"
31+
assert_eq!(Number::from_f64(12e40).unwrap().to_string(), "1.2e41");
3132
}
3233

3334
#[test]

0 commit comments

Comments
 (0)