Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 46 additions & 29 deletions doc/modules/ROOT/pages/format.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ https://www.boost.org/LICENSE_1_0.txt
= Formating support
:idprefix: format_

Boost.Decimal supports formatting with both `<format>` (when C++20 and header are both available) and `<fmt/format.h>`.
Boost.Decimal supports formatting with both `<format>` (when C++20 and header are both available), and `<fmt/format.h>` with all language standards.

[#std_format]
== `<format>`
Expand Down Expand Up @@ -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 <boost/decimal.hpp> // or <boost/decimal/format.hpp>
#include <format>
#include <iostream>

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]
== `<fmt/format.h>`

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 `<boost/decimal/fmt_format.hpp>` is *NOT* part of the convenience header, because it is an optional dependency on a potentially compiled library.

[source, c++]
Expand All @@ -123,12 +100,52 @@ The header `<boost/decimal/fmt_format.hpp>` 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 `<format>` by replacing namespaces `fmt::` with `std::`.
4 changes: 2 additions & 2 deletions examples/fmt_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 23 additions & 4 deletions examples/format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 2 additions & 4 deletions include/boost/decimal/format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
#ifndef BOOST_DECIMAL_FORMAT_HPP
#define BOOST_DECIMAL_FORMAT_HPP

// Many compilers seem to have <format> 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(<format>) && defined(__cpp_lib_format) && __cpp_lib_format >= 201907L && !defined(BOOST_DECIMAL_DISABLE_CLIB)

#define BOOST_DECIMAL_HAS_FORMAT_SUPPORT

Expand Down Expand Up @@ -315,6 +313,6 @@ struct formatter<T, CharT>

} // Namespace std

#endif
#endif // <format> compatibility

#endif //BOOST_DECIMAL_FORMAT_HPP