From 77d6036cd512f87eb992d8a3cfbd8a51044e3d4c Mon Sep 17 00:00:00 2001 From: Alex Alabuzhev Date: Fri, 10 May 2019 21:37:19 +0100 Subject: [PATCH] Fix unexpected trailing decimal point (#1153) --- include/fmt/format.h | 4 +++- test/printf-test.cc | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 0c462d13..c320abcf 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1187,12 +1187,14 @@ It grisu_prettify(const char* digits, int size, int exp, It it, } else if (full_exp > 0) { // 1234e-2 -> 12.34[0+] it = copy_str(digits, digits + full_exp, it); - *it++ = static_cast('.'); if (!params.trailing_zeros) { // Remove trailing zeros. while (size > full_exp && digits[size - 1] == '0') --size; + if (size != full_exp) + *it++ = static_cast('.'); return copy_str(digits + full_exp, digits + size, it); } + *it++ = static_cast('.'); it = copy_str(digits + full_exp, digits + size, it); if (params.num_digits > size) { // Add trailing zeros. diff --git a/test/printf-test.cc b/test/printf-test.cc index ab199d2c..52c7fc6a 100644 --- a/test/printf-test.cc +++ b/test/printf-test.cc @@ -406,6 +406,8 @@ TEST(PrintfTest, Float) { EXPECT_PRINTF(buffer, "%E", 392.65); EXPECT_PRINTF("392.65", "%g", 392.65); EXPECT_PRINTF("392.65", "%G", 392.65); + EXPECT_PRINTF("392", "%g", 392.0); + EXPECT_PRINTF("392", "%G", 392.0); safe_sprintf(buffer, "%a", -392.65); EXPECT_EQ(buffer, format("{:a}", -392.65)); safe_sprintf(buffer, "%A", -392.65);