From 69779b4ed6e78dfc5a87ee2fead6da3dcbf04134 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Tue, 24 Mar 2020 09:01:57 -0700 Subject: [PATCH] Fix handling of small precision in general format --- include/fmt/format.h | 6 ++++-- test/format-test.cc | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index bb9bc1e5..dc2531e8 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1152,9 +1152,11 @@ template class float_writer { // 1234e-6 -> 0.001234 *it++ = static_cast('0'); int num_zeros = -full_exp; - if (specs_.precision >= 0 && specs_.precision < num_zeros) - num_zeros = specs_.precision; int num_digits = num_digits_; + if (num_digits == 0 && specs_.precision >= 0 && + specs_.precision < num_zeros) { + num_zeros = specs_.precision; + } // Remove trailing zeros. if (!specs_.showpoint) while (num_digits > 0 && digits_[num_digits - 1] == '0') --num_digits; diff --git a/test/format-test.cc b/test/format-test.cc index a2223749..4e553022 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1125,6 +1125,7 @@ TEST(FormatterTest, Precision) { format("{:.838A}", -2.14001164E+38)); EXPECT_EQ("123.", format("{:#.0f}", 123.0)); EXPECT_EQ("1.23", format("{:.02f}", 1.234)); + EXPECT_EQ("0.001", format("{:.1g}", 0.001)); EXPECT_THROW_MSG(format("{0:.2}", reinterpret_cast(0xcafe)), format_error,