|
3 | 3 | // https://www.boost.org/LICENSE_1_0.txt |
4 | 4 |
|
5 | 5 | #include <boost/decimal.hpp> |
| 6 | +#include <boost/decimal/fmt_format.hpp> |
6 | 7 | #include <iostream> |
7 | 8 |
|
8 | 9 | #if defined(BOOST_DECIMAL_HAS_FMTLIB_SUPPORT) && defined(BOOST_DECIMAL_TEST_FMT) |
9 | 10 |
|
10 | | -#include <boost/decimal/fmt_format.hpp> |
11 | | - |
12 | 11 | int main() |
13 | 12 | { |
14 | 13 | constexpr boost::decimal::decimal64_t val1 {314, -2}; |
15 | 14 | constexpr boost::decimal::decimal32_t val2 {3141, -3}; |
16 | 15 |
|
| 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"; |
17 | 35 | 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'; |
19 | 37 |
|
20 | 38 | return 0; |
21 | 39 | } |
|
0 commit comments