From 4fd95e4b4d18be6be8fd8f6c8d685ad62e43de46 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 8 Aug 2020 08:14:39 -0700 Subject: [PATCH] Don't remove trailing zeros with # --- include/fmt/format-inl.h | 2 +- test/format-test.cc | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index e38ed16b..6d328845 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -1131,7 +1131,7 @@ int format_float(T value, int precision, float_specs specs, buffer& buf) { if (grisu_gen_digits(normalized, 1, exp, handler) == digits::error) return snprintf_float(value, precision, specs, buf); int num_digits = handler.size; - if (!fixed) { + if (!fixed && !specs.showpoint) { // Remove trailing zeros. while (num_digits > 0 && buf[num_digits - 1] == '0') { --num_digits; diff --git a/test/format-test.cc b/test/format-test.cc index bd3dcfdb..d0a38f25 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -761,6 +761,7 @@ TEST(FormatterTest, HashFlag) { EXPECT_EQ("-42.0", format("{0:#}", -42.0l)); EXPECT_EQ("4.e+01", format("{:#.0e}", 42.0)); EXPECT_EQ("0.", format("{:#.0f}", 0.01)); + EXPECT_EQ("0.50", format("{:#.2g}", 0.5)); auto s = format("{:#.0f}", 0.5); // MSVC's printf uses wrong rounding mode. EXPECT_TRUE(s == "0." || s == "1."); EXPECT_THROW_MSG(format("{0:#", 'c'), format_error,