Skip to content

Commit 1935654

Browse files
committed
Redo <format> example
1 parent f2bf3b6 commit 1935654

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

examples/format.cpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,30 @@
1111

1212
int main()
1313
{
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";
1736
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';
1938

2039
return 0;
2140
}

0 commit comments

Comments
 (0)