From ab0ba8a9d0ef694bfb346cd3b2f238e5e270d826 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 30 Jun 2019 08:44:59 -0700 Subject: [PATCH] Don't emit decimal point if there are no trailing digits (#1210) --- include/fmt/format.h | 8 +++++--- test/format-test.cc | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 550338075e4b..5679fdd870e6 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1096,14 +1096,16 @@ It grisu_prettify(const char* digits, int size, int exp, It it, } else { // 1234e-6 -> 0.001234 *it++ = static_cast('0'); - *it++ = static_cast('.'); int num_zeros = -full_exp; if (params.num_digits >= 0 && params.num_digits < num_zeros) num_zeros = params.num_digits; - it = std::fill_n(it, num_zeros, static_cast('0')); if (!params.trailing_zeros) while (size > 0 && digits[size - 1] == '0') --size; - it = copy_str(digits, digits + size, it); + if (num_zeros != 0 || size != 0) { + *it++ = static_cast('.'); + it = std::fill_n(it, num_zeros, static_cast('0')); + it = copy_str(digits, digits + size, it); + } } return it; } diff --git a/test/format-test.cc b/test/format-test.cc index da2fd721c30d..c5523deb00b9 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1453,6 +1453,7 @@ TEST(FormatterTest, FormatDouble) { TEST(FormatterTest, PrecisionRounding) { EXPECT_EQ("0", format("{:.0f}", 0.0)); + EXPECT_EQ("0", format("{:.0f}", 0.01)); EXPECT_EQ("0", format("{:.0f}", 0.1)); EXPECT_EQ("0.000", format("{:.3f}", 0.00049)); EXPECT_EQ("0.001", format("{:.3f}", 0.0005));