From 39971eb336d60c6dd66f7e10343977359fd4b14d Mon Sep 17 00:00:00 2001 From: Shawn Zhong Date: Sun, 15 Jan 2023 13:47:24 -0600 Subject: [PATCH] Fix localized format for float-point numbers (#3272) --- include/fmt/format.h | 2 +- test/xchar-test.cc | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index fef477a8..2a54044a 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -2637,7 +2637,7 @@ FMT_CONSTEXPR20 auto do_write_float(OutputIt out, const DecimalFP& f, int num_zeros = fspecs.showpoint ? fspecs.precision - significand_size : 0; size += 1 + to_unsigned(num_zeros > 0 ? num_zeros : 0); auto grouping = Grouping(loc, fspecs.locale); - size += to_unsigned(grouping.count_separators(significand_size)); + size += to_unsigned(grouping.count_separators(exp)); return write_padded(out, specs, size, [&](iterator it) { if (sign) *it++ = detail::sign(sign); it = write_significand(it, significand, significand_size, exp, diff --git a/test/xchar-test.cc b/test/xchar-test.cc index bda8c1a6..34af6e4f 100644 --- a/test/xchar-test.cc +++ b/test/xchar-test.cc @@ -438,6 +438,9 @@ TEST(locale_test, localized_double) { EXPECT_EQ(fmt::format(loc, "{:L}", 1234.5), "1~234?5"); EXPECT_EQ(fmt::format(loc, "{:L}", 12000.0), "12~000"); EXPECT_EQ(fmt::format(loc, "{:8L}", 1230.0), " 1~230"); + EXPECT_EQ(fmt::format(loc, "{:15.6Lf}", 0.1), " 0?100000"); + EXPECT_EQ(fmt::format(loc, "{:15.6Lf}", 1.0), " 1?000000"); + EXPECT_EQ(fmt::format(loc, "{:15.6Lf}", 1e3), " 1~000?000000"); } TEST(locale_test, format) {