//********************************************************
// The following code example is taken from the book
//  C++23 - The Complete Guide
//  by Nicolai M. Josuttis (www.josuttis.com)
//  https://www.cppstd23.com
//
// The code is licensed under a
//  Creative Commons Attribution 4.0 International License
//  https://creativecommons.org/licenses/by/4.0/
//********************************************************


#include <iostream>
#include <string>
#include <locale>
#include <format>

// define facet of locale with thousands separator:
struct ThousandsSep : std::numpunct<char> {
  char do_decimal_point() const override { return '.'; }     // decimal point is dot
  char do_thousands_sep() const override { return ','; }     // separate with commas
  std::string do_grouping() const override { return "\3"; }  // every 3 digits
};

int main()
{
  // patch std::cout locale to use thousands separator:
  std::locale locThSep{std::cout.getloc(), new ThousandsSep{}};
  std::cout.imbue(locThSep);

  // use locale to print numeric values:
  std::cout << std::format("{:10} {:16}\n", 1000000, 12345678.9999); 
  std::cout << std::format(locThSep, "{:10} {:16}\n", 1000000, 12345678.9999);
  std::cout << std::format(locThSep, "{:10L} {:16L}\n", 1000000, 12345678.9999);  
}

