lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [lmi] master 17f6042e 01/11: Test nonstandard "'" printf f


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 17f6042e 01/11: Test nonstandard "'" printf flag
Date: Fri, 29 Apr 2022 11:59:31 -0400 (EDT)

branch: master
commit 17f6042e8d08c887514af9de4c5157bd9b6bd99b
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>

    Test nonstandard "'" printf flag
    
    C++ has no thousands-separator flag, but "'" fulfills that function
    according to POSIX:
    
    https://pubs.opengroup.org/onlinepubs/9699919799/functions/fprintf.html
      The integer portion of the result of a decimal conversion
      ( %i, %d, %u, %f, %F, %g, or %G ) shall be formatted with
      thousands' grouping characters.
    
    With the gcc versions tested, this seems to have no effect when the
    'precision' is specified as zero. That's not good enough for lmi use.
---
 snprintf_test.cpp | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/snprintf_test.cpp b/snprintf_test.cpp
index 11464441..2beb3a69 100644
--- a/snprintf_test.cpp
+++ b/snprintf_test.cpp
@@ -23,6 +23,7 @@
 
 #include "test_tools.hpp"
 
+#include <clocale>                      // setlocale()
 #include <stdio.h>                      // snprintf()
 #include <string>
 
@@ -113,5 +114,23 @@ int test_main(int, char*[])
     int const number_of_digits = 16;
     LMI_TEST(0 == e.compare(0, number_of_digits, buf, 0, number_of_digits));
 
+    double v = 1234567.89;
+    std::setlocale(LC_NUMERIC, "");
+#if defined __GNUC__
+#   pragma GCC diagnostic push
+#   pragma GCC diagnostic ignored "-Wformat"
+#endif // defined __GNUC__
+    std::cout
+        << "snprintf with nonstandard \"'\" for thousands separators:"
+        << std::endl
+        ;
+    snprintf(buf, 999, "%'12.0f", v);
+    std::cout << "snprintf, 0 decimals: " << buf << std::endl;
+    snprintf(buf, 999, "%'12.2f", v);
+    std::cout << "snprintf, 2 decimals:  " << buf << std::endl;
+#if defined __GNUC__
+#   pragma GCC diagnostic pop
+#endif // defined __GNUC__
+
     return 0;
 }



reply via email to

[Prev in Thread] Current Thread [Next in Thread]