diff --git a/include/fmt/format.h b/include/fmt/format.h index 4543037a..d8e834f2 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -3060,6 +3060,7 @@ FMT_CONSTEXPR20 inline void format_dragon(basic_fp value, } int even = static_cast((value.f & 1) == 0); if (!upper) upper = &lower; + bool shortest = num_digits < 0; if ((flags & dragon::fixup) != 0) { if (add_compare(numerator, *upper, denominator) + even <= 0) { --exp10; @@ -3072,7 +3073,7 @@ FMT_CONSTEXPR20 inline void format_dragon(basic_fp value, if ((flags & dragon::fixed) != 0) adjust_precision(num_digits, exp10 + 1); } // Invariant: value == (numerator / denominator) * pow(10, exp10). - if (num_digits < 0) { + if (shortest) { // Generate the shortest representation. num_digits = 0; char* data = buf.data(); @@ -3102,7 +3103,7 @@ FMT_CONSTEXPR20 inline void format_dragon(basic_fp value, } // Generate the given number of digits. exp10 -= num_digits - 1; - if (num_digits == 0) { + if (num_digits <= 0) { denominator *= 10; auto digit = add_compare(numerator, numerator, denominator) > 0 ? '1' : '0'; buf.push_back(digit); @@ -3323,9 +3324,7 @@ FMT_CONSTEXPR20 auto format_float(Float value, int precision, float_specs specs, } // Compute the actual number of decimal digits to print. - if (fixed) { - adjust_precision(precision, exp + digits_in_the_first_segment); - } + if (fixed) adjust_precision(precision, exp + digits_in_the_first_segment); // Use Dragon4 only when there might be not enough digits in the first // segment. diff --git a/test/format-test.cc b/test/format-test.cc index 3e45a10f..baf33569 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1472,6 +1472,7 @@ TEST(format_test, format_infinity) { TEST(format_test, format_long_double) { EXPECT_EQ("0", fmt::format("{0:}", 0.0l)); EXPECT_EQ("0.000000", fmt::format("{0:f}", 0.0l)); + EXPECT_EQ("0.0", fmt::format("{:.1f}", 0.000000001l)); EXPECT_EQ("392.65", fmt::format("{0:}", 392.65l)); EXPECT_EQ("392.65", fmt::format("{0:g}", 392.65l)); EXPECT_EQ("392.65", fmt::format("{0:G}", 392.65l));