From ff4ddd199d2e1bde0d5d3093f2b9b86f871e449d Mon Sep 17 00:00:00 2001 From: Vladislav Shchapov Date: Wed, 20 Mar 2024 01:03:57 +0500 Subject: [PATCH] Fix group_digits for negative integers (#3901) Signed-off-by: Vladislav Shchapov --- include/fmt/format.h | 7 ++++--- test/format-test.cc | 3 +++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index a4a078999d3d..c8adf84b6c88 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -4130,9 +4130,10 @@ template struct formatter> : formatter { specs.width_ref, ctx); detail::handle_dynamic_spec( specs.precision, specs.precision_ref, ctx); - return detail::write_int(ctx.out(), - static_cast>(t.value), - 0, specs, detail::digit_grouping("\3", ",")); + auto arg = detail::make_write_int_arg(t.value, specs.sign); + return detail::write_int( + ctx.out(), static_cast>(arg.abs_value), + arg.prefix, specs, detail::digit_grouping("\3", ",")); } }; diff --git a/test/format-test.cc b/test/format-test.cc index 70624e835b88..c8293258cbef 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1837,6 +1837,9 @@ TEST(format_test, bytes) { TEST(format_test, group_digits_view) { EXPECT_EQ(fmt::format("{}", fmt::group_digits(10000000)), "10,000,000"); EXPECT_EQ(fmt::format("{:8}", fmt::group_digits(1000)), " 1,000"); + EXPECT_EQ(fmt::format("{}", fmt::group_digits(-10000000)), "-10,000,000"); + EXPECT_EQ(fmt::format("{:8}", fmt::group_digits(-1000)), " -1,000"); + EXPECT_EQ(fmt::format("{:8}", fmt::group_digits(-100)), " -100"); } #ifdef __cpp_generic_lambdas