@@ -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::`).
11592The 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
124101int 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::`.
0 commit comments