diff --git a/src/libcore/fmt/float.rs b/src/libcore/fmt/float.rs index 4825c2aa13264..03e7a9a49d8a7 100644 --- a/src/libcore/fmt/float.rs +++ b/src/libcore/fmt/float.rs @@ -32,22 +32,23 @@ fn float_to_decimal_common_exact(fmt: &mut Formatter, num: &T, // Don't inline this so callers that call both this and the above won't wind // up using the combined stack space of both functions in some cases. #[inline(never)] -fn float_to_decimal_common_shortest(fmt: &mut Formatter, - num: &T, sign: flt2dec::Sign) -> Result +fn float_to_decimal_common_shortest(fmt: &mut Formatter, num: &T, + sign: flt2dec::Sign, precision: usize) -> Result where T: flt2dec::DecodableFloat { unsafe { // enough for f32 and f64 let mut buf: [u8; flt2dec::MAX_SIG_DIGITS] = mem::uninitialized(); let mut parts: [flt2dec::Part; 4] = mem::uninitialized(); - let formatted = flt2dec::to_shortest_str(flt2dec::strategy::grisu::format_shortest, - *num, sign, 0, false, &mut buf, &mut parts); + let formatted = flt2dec::to_shortest_str(flt2dec::strategy::grisu::format_shortest, *num, + sign, precision, false, &mut buf, &mut parts); fmt.pad_formatted_parts(&formatted) } } // Common code of floating point Debug and Display. -fn float_to_decimal_common(fmt: &mut Formatter, num: &T, negative_zero: bool) -> Result +fn float_to_decimal_common(fmt: &mut Formatter, num: &T, + negative_zero: bool, min_precision: usize) -> Result where T: flt2dec::DecodableFloat { let force_sign = fmt.sign_plus(); @@ -61,7 +62,7 @@ fn float_to_decimal_common(fmt: &mut Formatter, num: &T, negative_zero: bool) if let Some(precision) = fmt.precision { float_to_decimal_common_exact(fmt, num, sign, precision) } else { - float_to_decimal_common_shortest(fmt, num, sign) + float_to_decimal_common_shortest(fmt, num, sign, min_precision) } } @@ -125,14 +126,14 @@ macro_rules! floating { #[stable(feature = "rust1", since = "1.0.0")] impl Debug for $ty { fn fmt(&self, fmt: &mut Formatter) -> Result { - float_to_decimal_common(fmt, self, true) + float_to_decimal_common(fmt, self, true, 1) } } #[stable(feature = "rust1", since = "1.0.0")] impl Display for $ty { fn fmt(&self, fmt: &mut Formatter) -> Result { - float_to_decimal_common(fmt, self, false) + float_to_decimal_common(fmt, self, false, 0) } } diff --git a/src/libcore/tests/fmt/float.rs b/src/libcore/tests/fmt/float.rs index 695001312e4d5..138c3970e9087 100644 --- a/src/libcore/tests/fmt/float.rs +++ b/src/libcore/tests/fmt/float.rs @@ -20,6 +20,8 @@ fn test_format_f64() { assert_eq!("1.23456789e3", format!("{:e}", 1234.56789f64)); assert_eq!("1.23456789E6", format!("{:E}", 1234567.89f64)); assert_eq!("1.23456789E3", format!("{:E}", 1234.56789f64)); + assert_eq!("0.0", format!("{:?}", 0.0f64)); + assert_eq!("1.01", format!("{:?}", 1.01f64)); } #[test] @@ -34,4 +36,6 @@ fn test_format_f32() { assert_eq!("1.2345679e3", format!("{:e}", 1234.56789f32)); assert_eq!("1.2345679E6", format!("{:E}", 1234567.89f32)); assert_eq!("1.2345679E3", format!("{:E}", 1234.56789f32)); + assert_eq!("0.0", format!("{:?}", 0.0f32)); + assert_eq!("1.01", format!("{:?}", 1.01f32)); } diff --git a/src/test/run-pass/ifmt.rs b/src/test/run-pass/ifmt.rs index 08e9990511fbb..d09376acc84ae 100644 --- a/src/test/run-pass/ifmt.rs +++ b/src/test/run-pass/ifmt.rs @@ -158,8 +158,8 @@ pub fn main() { // Float edge cases t!(format!("{}", -0.0), "0"); - t!(format!("{:?}", -0.0), "-0"); - t!(format!("{:?}", 0.0), "0"); + t!(format!("{:?}", -0.0), "-0.0"); + t!(format!("{:?}", 0.0), "0.0"); // sign aware zero padding t!(format!("{:<3}", 1), "1 ");