diff --git a/doc/modules/ROOT/pages/format.adoc b/doc/modules/ROOT/pages/format.adoc index 3695f181d..c9468a6e7 100644 --- a/doc/modules/ROOT/pages/format.adoc +++ b/doc/modules/ROOT/pages/format.adoc @@ -8,7 +8,7 @@ https://www.boost.org/LICENSE_1_0.txt = Formating support :idprefix: format_ -Boost.Decimal supports formatting with both `` (when C++20 and header are both available) and ``. +Boost.Decimal supports formatting with both `` (when C++20 and header are both available), and `` with all language standards. [#std_format] == `` @@ -87,31 +87,8 @@ String literal pass:["{Sign, Padding, Precision, Type, Locale}"] === Examples -The example is padding modifiers can be done like so +This example can be found in the examples/ folder as https://github.com/cppalliance/decimal/blob/develop/examples/fmt_format.cpp[fmt_format.cpp] -[source, c++] ----- -#include // or -#include -#include - -int main() -{ - constexpr boost::decimal::decimal64_t val1 {314, -2}; - constexpr boost::decimal::decimal32_t val2 {3141, -3}; - - std::cout << std::format("{:10.3e}", val1) << '\n'; - std::cout << std::format("{:10.3e}", val2) << std::endl; - - return 0; -} ----- - -[#fmt_format] -== `` - -Support for pass:[{fmt}] is also available. -All the above information on modifiers is the same for fmtlib, just in a different namespace (i.e. `fmt::` instead of `std::`). The header `` is *NOT* part of the convenience header, because it is an optional dependency on a potentially compiled library. [source, c++] @@ -123,12 +100,52 @@ The header `` is *NOT* part of the convenience hea int main() { - constexpr boost::decimal::decimal64_t val1 {314, -2}; - constexpr boost::decimal::decimal32_t val2 {3141, -3}; - + constexpr boost::decimal::decimal64_t val1 {"3.14"}; + constexpr boost::decimal::decimal32_t val2 {"3.141"}; + + // The easiest is no specification which is general format + // Given these values they will print in fixed format + std::cout << "Default Format:\n"; + std::cout << fmt::format("{}", val1) << '\n'; + std::cout << fmt::format("{}", val2) << "\n\n"; + + // Next we can add a type modifier to get scientific formatting + std::cout << "Scientific Format:\n"; + std::cout << fmt::format("{:e}", val1) << '\n'; + std::cout << fmt::format("{:e}", val2) << "\n\n"; + + // Next we can add a type modifier to get scientific formatting + // Here this gives one digit of precision rounded according to current rounding mode + std::cout << "Scientific Format with Specified Precision:\n"; + std::cout << fmt::format("{:.1e}", val1) << '\n'; + std::cout << fmt::format("{:.1e}", val2) << "\n\n"; + + // This combines the padding modifier (10), precision (3 digits), and a type modifier (e) + std::cout << "Scientific Format with Specified Precision and Padding:\n"; std::cout << fmt::format("{:10.3e}", val1) << '\n'; - std::cout << fmt::format("{:10.3e}", val2) << std::endl; + std::cout << fmt::format("{:10.3e}", val2) << '\n'; return 0; } ---- + +Output: +---- +Default Format: +3.14 +3.141 + +Scientific Format: +3.14e+00 +3.141e+00 + +Scientific Format with Specified Precision: +3.1e+00 +3.1e+00 + +Scientific Format with Specified Precision and Padding: +03.140e+00 +03.141e+00 +---- + +This same example can be run with `` by replacing namespaces `fmt::` with `std::`. diff --git a/examples/fmt_format.cpp b/examples/fmt_format.cpp index 792dc6316..3fa18214d 100644 --- a/examples/fmt_format.cpp +++ b/examples/fmt_format.cpp @@ -10,8 +10,8 @@ int main() { - constexpr boost::decimal::decimal64_t val1 {314, -2}; - constexpr boost::decimal::decimal32_t val2 {3141, -3}; + constexpr boost::decimal::decimal64_t val1 {"3.14"}; + constexpr boost::decimal::decimal32_t val2 {"3.141"}; // The easiest is no specification which is general format // Given these values they will print in fixed format diff --git a/examples/format.cpp b/examples/format.cpp index e2b3b68c2..41dd880f8 100644 --- a/examples/format.cpp +++ b/examples/format.cpp @@ -11,11 +11,30 @@ int main() { - constexpr boost::decimal::decimal64_t val1 {314, -2}; - constexpr boost::decimal::decimal32_t val2 {3141, -3}; - + constexpr boost::decimal::decimal64_t val1 {"3.14"}; + constexpr boost::decimal::decimal32_t val2 {"3.141"}; + + // The easiest is no specification which is general format + // Given these values they will print in fixed format + std::cout << "Default Format:\n"; + std::cout << std::format("{}", val1) << '\n'; + std::cout << std::format("{}", val2) << "\n\n"; + + // Next we can add a type modifier to get scientific formatting + std::cout << "Scientific Format:\n"; + std::cout << std::format("{:e}", val1) << '\n'; + std::cout << std::format("{:e}", val2) << "\n\n"; + + // Next we can add a type modifier to get scientific formatting + // Here this gives one digit of precision rounded according to current rounding mode + std::cout << "Scientific Format with Specified Precision:\n"; + std::cout << std::format("{:.1e}", val1) << '\n'; + std::cout << std::format("{:.1e}", val2) << "\n\n"; + + // This combines the padding modifier (10), precision (3 digits), and a type modifier (e) + std::cout << "Scientific Format with Specified Precision and Padding:\n"; std::cout << std::format("{:10.3e}", val1) << '\n'; - std::cout << std::format("{:10.3e}", val2) << std::endl; + std::cout << std::format("{:10.3e}", val2) << '\n'; return 0; } diff --git a/include/boost/decimal/format.hpp b/include/boost/decimal/format.hpp index 8a740d581..7f66d5176 100644 --- a/include/boost/decimal/format.hpp +++ b/include/boost/decimal/format.hpp @@ -5,9 +5,7 @@ #ifndef BOOST_DECIMAL_FORMAT_HPP #define BOOST_DECIMAL_FORMAT_HPP -// Many compilers seem to have with completely broken support so narrow down our support range -#if (__cplusplus >= 202002L || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)) && !defined(BOOST_DECIMAL_DISABLE_CLIB) && \ - ((defined(__GNUC__) && __GNUC__ >= 13) || (defined(__clang__) && __clang_major__ >= 18) || (defined(_MSC_VER) && _MSC_VER >= 1940)) +#if __has_include() && defined(__cpp_lib_format) && __cpp_lib_format >= 201907L && !defined(BOOST_DECIMAL_DISABLE_CLIB) #define BOOST_DECIMAL_HAS_FORMAT_SUPPORT @@ -315,6 +313,6 @@ struct formatter } // Namespace std -#endif +#endif // compatibility #endif //BOOST_DECIMAL_FORMAT_HPP