Formatted Output of Floating-Point Values Using std::ostream
Question:
How can I achieve precise control over the formatting of floating-point values using std::ostream, similar to the printf_s function shown below?
<code class="cpp">printf_s("%11.6lf", my_double); // Prints " 42.000000"</code>
Answer:
To format floating-point values with std::cout, utilize stream manipulators. The following code achieves the desired output:
<code class="cpp">std::cout << std::fixed << std::setw(11) << std::setprecision(6) << my_double;</code>
Explanation:
Additional Resources:
For comprehensive documentation on std::ostream formatting, refer to the following resources:
The above is the detailed content of How to Achieve Precise Floating-Point Formatting with std::ostream?. For more information, please follow other related articles on the PHP Chinese website!