Skip to content

Commit e24243f

Browse files
committed
Update our fmt formatting example
1 parent 04a51a5 commit e24243f

File tree

2 files changed

+68
-18
lines changed

2 files changed

+68
-18
lines changed

doc/modules/ROOT/pages/examples.adoc

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -153,48 +153,95 @@ This example could be extended with the simple moving average to create full ban
153153
== Formatting
154154

155155
Boost.Decimal allows you to format your output with both `<format>` and `<fmt/format.h>` depending on your compiler support.
156+
pass:[{fmt}] support is available starting with pass:[C++14] so long as you have the library available, but `<format>` requires pass:[C++20] and compiler support
156157

157-
[#examples_std_format]
158-
=== `<format>`
158+
[#examples_fmt_format]
159+
=== `<fmt/format.hpp>`
159160

160-
If your compiler provides `<format>` you can use that to format the output of your values:
161+
We also provide support for pass:[{fmt}] so you can easily just swap the namespaces and headers on the above example:
161162

162163
[source, c++]
163164
----
164-
#include <boost/decimal.hpp>
165+
// Copyright 2025 Matt Borland
166+
// Distributed under the Boost Software License, Version 1.0.
167+
// https://www.boost.org/LICENSE_1_0.txt
168+
//
169+
// This example demonstrates usage and formatting of decimal types with fmt
170+
171+
#include <boost/decimal/decimal32_t.hpp> // For type decimal32_t
172+
#include <boost/decimal/decimal64_t.hpp> // For type decimal64_t
173+
#include <boost/decimal/fmt_format.hpp> // For {fmt} support
165174
#include <iostream>
166-
#include <format>
167175
168176
int main()
169177
{
170-
constexpr boost::decimal::decimal64_t val1 {314, -2};
171-
constexpr boost::decimal::decimal32_t val2 {3141, -3};
172-
173-
std::cout << std::format("{:10.3e}", val1) << '\n';
174-
std::cout << std::format("{:10.3e}", val2) << std::endl;
178+
constexpr boost::decimal::decimal64_t val1 {"3.14"};
179+
constexpr boost::decimal::decimal32_t val2 {"3.141"};
180+
181+
// The easiest is no specification which is general format
182+
// Given these values they will print in fixed format
183+
std::cout << "Default Format:\n";
184+
std::cout << fmt::format("{}", val1) << '\n';
185+
std::cout << fmt::format("{}", val2) << "\n\n";
186+
187+
// Next we can add a type modifier to get scientific formatting
188+
std::cout << "Scientific Format:\n";
189+
std::cout << fmt::format("{:e}", val1) << '\n';
190+
std::cout << fmt::format("{:e}", val2) << "\n\n";
191+
192+
// Next we can add a type modifier to get scientific formatting
193+
// Here this gives one digit of precision rounded according to current rounding mode
194+
std::cout << "Scientific Format with Specified Precision:\n";
195+
std::cout << fmt::format("{:.1e}", val1) << '\n';
196+
std::cout << fmt::format("{:.1e}", val2) << "\n\n";
197+
198+
// This combines the padding modifier (10), precision (3 digits), and a type modifier (e)
199+
std::cout << "Scientific Format with Specified Precision and Padding:\n";
200+
std::cout << fmt::format("{:10.3e}", val1) << '\n';
201+
std::cout << fmt::format("{:10.3e}", val2) << '\n';
175202
176203
return 0;
177204
}
178205
----
179206

180-
[#examples_fmt_format]
181-
=== `<fmt/format.hpp>`
207+
Expected Output:
208+
----
209+
Default Format:
210+
3.14
211+
3.141
182212
183-
We also provide support for pass:[{fmt}] so you can easily just swap the namespaces and headers on the above example:
213+
Scientific Format:
214+
3.14e+00
215+
3.141e+00
216+
217+
Scientific Format with Specified Precision:
218+
3.1e+00
219+
3.1e+00
220+
221+
Scientific Format with Specified Precision and Padding:
222+
03.140e+00
223+
03.141e+00
224+
----
225+
226+
[#examples_std_format]
227+
=== `<format>`
228+
229+
Taking the above example of pass:[{fmt}] and replacing all instances of `namespace fmt` with `namespace std` gives us another working example.
230+
This example can be found in the `examples/` folder as https://github.com/cppalliance/decimal/blob/develop/examples/format.cpp[format.cpp]
184231

185232
[source, c++]
186233
----
187234
#include <boost/decimal.hpp>
188235
#include <iostream>
189-
#include <fmt/format.h>
236+
#include <format>
190237
191238
int main()
192239
{
193240
constexpr boost::decimal::decimal64_t val1 {314, -2};
194241
constexpr boost::decimal::decimal32_t val2 {3141, -3};
195242
196-
std::cout << fmt::format("{:10.3e}", val1) << '\n';
197-
std::cout << fmt::format("{:10.3e}", val2) << std::endl;
243+
std::cout << std::format("{:10.3e}", val1) << '\n';
244+
std::cout << std::format("{:10.3e}", val2) << std::endl;
198245
199246
return 0;
200247
}

examples/fmt_format.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
// Copyright 2025 Matt Borland
22
// Distributed under the Boost Software License, Version 1.0.
33
// https://www.boost.org/LICENSE_1_0.txt
4+
//
5+
// This example demonstrates usage and formatting of decimal types with fmt
46

5-
#include <boost/decimal.hpp>
6-
#include <boost/decimal/fmt_format.hpp>
7+
#include <boost/decimal/decimal32_t.hpp> // For type decimal32_t
8+
#include <boost/decimal/decimal64_t.hpp> // For type decimal64_t
9+
#include <boost/decimal/fmt_format.hpp> // For {fmt} support
710
#include <iostream>
811

912
#if defined(BOOST_DECIMAL_HAS_FMTLIB_SUPPORT) && defined(BOOST_DECIMAL_TEST_FMT)

0 commit comments

Comments
 (0)