diff --git a/include/fmt/std.h b/include/fmt/std.h index 2d6012e3..7085cae8 100644 --- a/include/fmt/std.h +++ b/include/fmt/std.h @@ -595,10 +595,15 @@ struct formatter, Char> : nested_formatter { template FMT_CONSTEXPR auto operator()(OutputIt out) -> OutputIt { - auto format = detail::string_literal{}; - return fmt::format_to(out, basic_string_view(format), - f->nested(c.real()), f->nested(c.imag())); + if (c.real() != 0) { + auto format_full = detail::string_literal{}; + return fmt::format_to(out, basic_string_view(format_full), + f->nested(c.real()), f->nested(c.imag())); + } + auto format_imag = detail::string_literal{}; + return fmt::format_to(out, basic_string_view(format_imag), + f->nested(c.imag())); } }; diff --git a/test/std-test.cc b/test/std-test.cc index 3fbcedf5..691b1097 100644 --- a/test/std-test.cc +++ b/test/std-test.cc @@ -67,6 +67,7 @@ TEST(std_test, thread_id) { TEST(std_test, complex) { EXPECT_EQ(fmt::format("{}", std::complex(1, 2.2)), "(1+2.2i)"); + EXPECT_EQ(fmt::format("{}", std::complex(0, 2.2)), "2.2i"); EXPECT_EQ(fmt::format("{:>20.2f}", std::complex(1, 2.2)), " (1.00+2.20i)"); }