Skip to content

Commit 9b89544

Browse files
committed
Expand example
1 parent bb266ac commit 9b89544

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

examples/fmt_format.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,37 @@
33
// https://www.boost.org/LICENSE_1_0.txt
44

55
#include <boost/decimal.hpp>
6+
#include <boost/decimal/fmt_format.hpp>
67
#include <iostream>
78

89
#if defined(BOOST_DECIMAL_HAS_FMTLIB_SUPPORT) && defined(BOOST_DECIMAL_TEST_FMT)
910

10-
#include <boost/decimal/fmt_format.hpp>
11-
1211
int main()
1312
{
1413
constexpr boost::decimal::decimal64_t val1 {314, -2};
1514
constexpr boost::decimal::decimal32_t val2 {3141, -3};
1615

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

2038
return 0;
2139
}

0 commit comments

Comments
 (0)