Skip to content

Commit c549ec3

Browse files
authored
Merge pull request #1222 from cppalliance/format_reqs
2 parents 9d09df9 + e365f70 commit c549ec3

File tree

4 files changed

+73
-39
lines changed

4 files changed

+73
-39
lines changed

doc/modules/ROOT/pages/format.adoc

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ https://www.boost.org/LICENSE_1_0.txt
88
= Formating support
99
:idprefix: format_
1010

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

1313
[#std_format]
1414
== `<format>`
@@ -87,31 +87,8 @@ String literal pass:["{Sign, Padding, Precision, Type, Locale}"]
8787

8888
=== Examples
8989

90-
The example is padding modifiers can be done like so
90+
This example can be found in the examples/ folder as https://github.com/cppalliance/decimal/blob/develop/examples/fmt_format.cpp[fmt_format.cpp]
9191

92-
[source, c++]
93-
----
94-
#include <boost/decimal.hpp> // or <boost/decimal/format.hpp>
95-
#include <format>
96-
#include <iostream>
97-
98-
int main()
99-
{
100-
constexpr boost::decimal::decimal64_t val1 {314, -2};
101-
constexpr boost::decimal::decimal32_t val2 {3141, -3};
102-
103-
std::cout << std::format("{:10.3e}", val1) << '\n';
104-
std::cout << std::format("{:10.3e}", val2) << std::endl;
105-
106-
return 0;
107-
}
108-
----
109-
110-
[#fmt_format]
111-
== `<fmt/format.h>`
112-
113-
Support for pass:[{fmt}] is also available.
114-
All the above information on modifiers is the same for fmtlib, just in a different namespace (i.e. `fmt::` instead of `std::`).
11592
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.
11693

11794
[source, c++]
@@ -123,12 +100,52 @@ The header `<boost/decimal/fmt_format.hpp>` is *NOT* part of the convenience hea
123100
124101
int main()
125102
{
126-
constexpr boost::decimal::decimal64_t val1 {314, -2};
127-
constexpr boost::decimal::decimal32_t val2 {3141, -3};
128-
103+
constexpr boost::decimal::decimal64_t val1 {"3.14"};
104+
constexpr boost::decimal::decimal32_t val2 {"3.141"};
105+
106+
// The easiest is no specification which is general format
107+
// Given these values they will print in fixed format
108+
std::cout << "Default Format:\n";
109+
std::cout << fmt::format("{}", val1) << '\n';
110+
std::cout << fmt::format("{}", val2) << "\n\n";
111+
112+
// Next we can add a type modifier to get scientific formatting
113+
std::cout << "Scientific Format:\n";
114+
std::cout << fmt::format("{:e}", val1) << '\n';
115+
std::cout << fmt::format("{:e}", val2) << "\n\n";
116+
117+
// Next we can add a type modifier to get scientific formatting
118+
// Here this gives one digit of precision rounded according to current rounding mode
119+
std::cout << "Scientific Format with Specified Precision:\n";
120+
std::cout << fmt::format("{:.1e}", val1) << '\n';
121+
std::cout << fmt::format("{:.1e}", val2) << "\n\n";
122+
123+
// This combines the padding modifier (10), precision (3 digits), and a type modifier (e)
124+
std::cout << "Scientific Format with Specified Precision and Padding:\n";
129125
std::cout << fmt::format("{:10.3e}", val1) << '\n';
130-
std::cout << fmt::format("{:10.3e}", val2) << std::endl;
126+
std::cout << fmt::format("{:10.3e}", val2) << '\n';
131127
132128
return 0;
133129
}
134130
----
131+
132+
Output:
133+
----
134+
Default Format:
135+
3.14
136+
3.141
137+
138+
Scientific Format:
139+
3.14e+00
140+
3.141e+00
141+
142+
Scientific Format with Specified Precision:
143+
3.1e+00
144+
3.1e+00
145+
146+
Scientific Format with Specified Precision and Padding:
147+
03.140e+00
148+
03.141e+00
149+
----
150+
151+
This same example can be run with `<format>` by replacing namespaces `fmt::` with `std::`.

examples/fmt_format.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
int main()
1212
{
13-
constexpr boost::decimal::decimal64_t val1 {314, -2};
14-
constexpr boost::decimal::decimal32_t val2 {3141, -3};
13+
constexpr boost::decimal::decimal64_t val1 {"3.14"};
14+
constexpr boost::decimal::decimal32_t val2 {"3.141"};
1515

1616
// The easiest is no specification which is general format
1717
// Given these values they will print in fixed format

examples/format.cpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,30 @@
1111

1212
int main()
1313
{
14-
constexpr boost::decimal::decimal64_t val1 {314, -2};
15-
constexpr boost::decimal::decimal32_t val2 {3141, -3};
16-
14+
constexpr boost::decimal::decimal64_t val1 {"3.14"};
15+
constexpr boost::decimal::decimal32_t val2 {"3.141"};
16+
17+
// The easiest is no specification which is general format
18+
// Given these values they will print in fixed format
19+
std::cout << "Default Format:\n";
20+
std::cout << std::format("{}", val1) << '\n';
21+
std::cout << std::format("{}", val2) << "\n\n";
22+
23+
// Next we can add a type modifier to get scientific formatting
24+
std::cout << "Scientific Format:\n";
25+
std::cout << std::format("{:e}", val1) << '\n';
26+
std::cout << std::format("{:e}", val2) << "\n\n";
27+
28+
// Next we can add a type modifier to get scientific formatting
29+
// Here this gives one digit of precision rounded according to current rounding mode
30+
std::cout << "Scientific Format with Specified Precision:\n";
31+
std::cout << std::format("{:.1e}", val1) << '\n';
32+
std::cout << std::format("{:.1e}", val2) << "\n\n";
33+
34+
// This combines the padding modifier (10), precision (3 digits), and a type modifier (e)
35+
std::cout << "Scientific Format with Specified Precision and Padding:\n";
1736
std::cout << std::format("{:10.3e}", val1) << '\n';
18-
std::cout << std::format("{:10.3e}", val2) << std::endl;
37+
std::cout << std::format("{:10.3e}", val2) << '\n';
1938

2039
return 0;
2140
}

include/boost/decimal/format.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
#ifndef BOOST_DECIMAL_FORMAT_HPP
66
#define BOOST_DECIMAL_FORMAT_HPP
77

8-
// Many compilers seem to have <format> with completely broken support so narrow down our support range
9-
#if (__cplusplus >= 202002L || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)) && !defined(BOOST_DECIMAL_DISABLE_CLIB) && \
10-
((defined(__GNUC__) && __GNUC__ >= 13) || (defined(__clang__) && __clang_major__ >= 18) || (defined(_MSC_VER) && _MSC_VER >= 1940))
8+
#if __has_include(<format>) && defined(__cpp_lib_format) && __cpp_lib_format >= 201907L && !defined(BOOST_DECIMAL_DISABLE_CLIB)
119

1210
#define BOOST_DECIMAL_HAS_FORMAT_SUPPORT
1311

@@ -315,6 +313,6 @@ struct formatter<T, CharT>
315313

316314
} // Namespace std
317315

318-
#endif
316+
#endif // <format> compatibility
319317

320318
#endif //BOOST_DECIMAL_FORMAT_HPP

0 commit comments

Comments
 (0)