Use the locale decimal point in localized hexfloat formatting by INo-xious · Pull Request #4992 · fmtlib/fmt · GitHub
Skip to content

Use the locale decimal point in localized hexfloat formatting - #4992

Open
INo-xious wants to merge 1 commit into
fmtlib:mainfrom
INo-xious:fix/hexfloat-locale-point
Open

INo-xious wants to merge 1 commit into
fmtlib:mainfrom
INo-xious:fix/hexfloat-locale-point

Conversation

@INo-xious

Copy link
Copy Markdown

With the L flag, floating-point values use the locale's decimal point (as documented in syntax.md: "inserts the locale's digit grouping characters and — for floating-point values — its decimal point"), but the hexfloat presentation ignores it:

// numpunct with decimal_point '?' and thousands_sep '~'
fmt::format(loc, "{:L}",  1234.5);  // "1~234?5"
fmt::format(loc, "{:Le}", 1234.5);  // "1?234500e+03"
fmt::format(loc, "{:La}", 1234.5);  // "0x1.34ap+10", expected "0x1?34ap+10"

For comparison, std::format(loc, "{:La}", 1234.5) with libstdc++ gives 1?34ap+10 (without the 0x prefix, which fmt always emits).

The hexfloat branch of write wrote the buffer produced by format_hexfloat directly. It now writes it through write_padded and replaces the . with decimal_point<Char>(loc) when the value is localized, using the same FMT_USE_LOCALE && specs.localized() condition as do_write_float. No digit grouping is applied to hex digits, the 0x prefix and the sign-aware zero padding from #4972 are unchanged, and non-localized output is byte-for-byte the same.

Tests: added {:La} and {:+012La} cases to locale_test.localized_double and a wide {:La} case to locale_test.wformat in test/xchar-test.cc, next to the existing tests that use the same numpunct facet. They fail on main ("0x1.34ap+10", "+0x0001.8p+0", L"0x1.8p+0") and pass with the change. The full ctest suite passes with g++ 13 in C++17 (FMT_PEDANTIC=ON, FMT_WERROR=ON), format-test, xchar-test and nolocale-test pass in C++11, and clang-format 21 reports no changes.

With the L flag, floating-point values are formatted with the locale's
decimal point, but the hexfloat presentation ('a'/'A') always emitted
'.'. Replace it with the locale decimal point when the value is
localized, like the other floating-point presentations do.
@INo-xious
INo-xious requested a review from vitaut as a code owner October 19, 2026 02:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant