[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;
}
- [lmi-commits] [lmi] master updated (a2c3a37c -> 92d90368), Greg Chicares, 2022/04/29
- [lmi-commits] [lmi] master 17f6042e 01/11: Test nonstandard "'" printf flag,
Greg Chicares <=
- [lmi-commits] [lmi] master 5e493f40 02/11: Rename ncnnnpnn, Greg Chicares, 2022/04/29
- [lmi-commits] [lmi] master a87f01b0 03/11: Rename a function, Greg Chicares, 2022/04/29
- [lmi-commits] [lmi] master 51c6865b 04/11: Use non-long double for duff_fmt(), Greg Chicares, 2022/04/29
- [lmi-commits] [lmi] master e8d7f191 05/11: Use low-level functions for low-level work, Greg Chicares, 2022/04/29
- [lmi-commits] [lmi] master 2202d220 09/11: Reimplement ledger_format() in terms of duff_fmt(), Greg Chicares, 2022/04/29
- [lmi-commits] [lmi] master d46e3440 07/11: Modernize duff_fmt(), Greg Chicares, 2022/04/29
- [lmi-commits] [lmi] master d838ad1f 06/11: Make number of decimals a duff_fmt() argument, Greg Chicares, 2022/04/29
- [lmi-commits] [lmi] master e8d4b982 08/11: Measure ledger_format() speed, Greg Chicares, 2022/04/29
- [lmi-commits] [lmi] master 92d90368 11/11: Designate release candidate 20220429T1437Z, Greg Chicares, 2022/04/29
- [lmi-commits] [lmi] master 542c0473 10/11: Augment unit test, Greg Chicares, 2022/04/29