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 localized format for float-point numbers #3272

Merged
merged 1 commit into from
Jan 15, 2023

Conversation

ShawnZhong
Copy link
Contributor

Fix #3263

The following piece of code used to print 15, 9, 14, which should be 15, 15, 15. This PR fixes this issue.

#include <locale>
#include <fmt/format.h>

template <typename Char> struct numpunct : std::numpunct<Char> {
 protected:
  Char do_decimal_point() const override { return '.'; }
  std::string do_grouping() const override { return "\1"; }
  Char do_thousands_sep() const override { return ','; }
};

int main() {
  auto loc = std::locale(std::locale(), new numpunct<char>());
  auto i = fmt::format(loc, "{:15.6Lf}", 0.1).size();
  auto j = fmt::format(loc, "{:15.6Lf}", 1.0).size();
  auto k = fmt::format(loc, "{:15.6Lf}", 1e3).size();
  fmt::print("{}, {}, {}", i, j, k);
}

@vitaut vitaut merged commit 39971eb into fmtlib:master Jan 15, 2023
@vitaut
Copy link
Contributor

vitaut commented Jan 15, 2023

Thank you!

@ShawnZhong ShawnZhong deleted the loc_align branch January 16, 2023 01:52
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.

Alignment of floating-point numbers is incorrect if the output is localized and the integer part is zero
2 participants