diff --git a/libgnucash/engine/gnc-numeric.cpp b/libgnucash/engine/gnc-numeric.cpp index 0fc47d5687..3d540056b8 100644 --- a/libgnucash/engine/gnc-numeric.cpp +++ b/libgnucash/engine/gnc-numeric.cpp @@ -1303,6 +1303,18 @@ main(int argc, char ** argv) } #endif + +std::ostream& +operator<<(std::ostream& s, GncNumeric n) +{ + std::basic_ostringstream ss; + ss.imbue(s.getloc()); + ss << n; + std::wstring_convert> make_utf8; + s << make_utf8.to_bytes(ss.str()); + return s; +} + const char* gnc_numeric_errorCode_to_string(GNCNumericErrorCode error_code) { switch (error_code) diff --git a/libgnucash/engine/gnc-numeric.hpp b/libgnucash/engine/gnc-numeric.hpp index d9ff3d7a75..a22f89bd43 100644 --- a/libgnucash/engine/gnc-numeric.hpp +++ b/libgnucash/engine/gnc-numeric.hpp @@ -27,6 +27,7 @@ #include #include #include // For std::bad_cast exception +#include // UTF-8 <--> UTF16 conversion #include "gnc-rational-rounding.hpp" class GncRational; @@ -339,6 +340,7 @@ inline GncNumeric operator/(int64_t a, GncNumeric b) * decimal if the denominator is a multiple of 10, otherwise as * "numerator/denominator". */ +std::ostream& operator<<(std::ostream&, GncNumeric); /* Implementation adapted from "The C++ Standard Library, Second Edition" by * Nicolai M. Josuttis, Addison-Wesley, 2012, ISBN 978-0-321-62321-8. @@ -349,10 +351,10 @@ std::basic_ostream& operator<<(std::basic_ostream& std::basic_ostringstream ss; std::locale loc = s.getloc(); ss.imbue(loc); - char dec_pt = '.'; + auto dec_pt = static_cast('.'); try { - dec_pt = std::use_facet>(loc).decimal_point(); + dec_pt = std::use_facet>(loc).decimal_point(); } catch(const std::bad_cast& err) {} //Don't do anything, num_sep is already set. @@ -369,7 +371,6 @@ std::basic_ostream& operator<<(std::basic_ostream& return s; } - /** * std::stream input operator. * @@ -386,7 +387,7 @@ std::basic_ostream& operator<<(std::basic_ostream& template std::basic_istream& operator>>(std::basic_istream& s, GncNumeric& n) { - std::string instr; + std::basic_string instr; s >> instr; if (s) n = GncNumeric(instr, true); diff --git a/libgnucash/engine/test/gtest-gnc-numeric.cpp b/libgnucash/engine/test/gtest-gnc-numeric.cpp index 82d6b8fa4a..8326071f41 100644 --- a/libgnucash/engine/test/gtest-gnc-numeric.cpp +++ b/libgnucash/engine/test/gtest-gnc-numeric.cpp @@ -213,10 +213,15 @@ TEST(gncnumeric_stream, output_stream) EXPECT_EQ("123/456", output.str()); try { - output.imbue(std::locale("fr_FR.utf8")); + auto loc = std::locale("fr_FR.utf8"); + auto thou_sep = std::use_facet>(loc).thousands_sep(); + output.imbue(loc); output.str(""); output << simple_int; - EXPECT_EQ("123 456", output.str()); + if (thou_sep == L' ') + EXPECT_EQ("123 456", output.str()); + else + EXPECT_EQ("123\xe2\x80\xaf""456", output.str()); } catch (std::runtime_error) {