|
11 | 11 |
|
12 | 12 | int main() |
13 | 13 | { |
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"; |
17 | 36 | 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'; |
19 | 38 |
|
20 | 39 | return 0; |
21 | 40 | } |
|
0 commit comments