Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix group_digits for negative integers #3901

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -4130,9 +4130,10 @@ template <typename T> struct formatter<group_digits_view<T>> : formatter<T> {
specs.width_ref, ctx);
detail::handle_dynamic_spec<detail::precision_checker>(
specs.precision, specs.precision_ref, ctx);
return detail::write_int(ctx.out(),
static_cast<detail::uint64_or_128_t<T>>(t.value),
0, specs, detail::digit_grouping<char>("\3", ","));
auto arg = detail::make_write_int_arg(t.value, specs.sign);
return detail::write_int(
ctx.out(), static_cast<detail::uint64_or_128_t<T>>(arg.abs_value),
arg.prefix, specs, detail::digit_grouping<char>("\3", ","));
}
};

Expand Down
3 changes: 3 additions & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading