Home > Article > Backend Development > How to Format Floating Point Numbers with std::cout?
Formatting Floating Point Numbers with std::cout
This question addresses the challenges of formatting floating-point numbers using std::cout, a fundamental output stream in C . The user expresses frustration with the limited formatting options available through std::cout and contemplates resorting to the more verbose sprintf_s function. They seek a comprehensive reference that consolidates formatting capabilities for std::ostream.
Answer:
Fortunately, std::cout provides stream manipulators that offer flexible formatting options for floating-point numbers. These manipulators include:
Example:
The desired output of " 42.000000" can be achieved using the following code:
<code class="cpp">#include <iomanip> std::cout << std::fixed << std::setw(11) << std::setprecision(6) << my_double;</code>
Additional Notes:
The above is the detailed content of How to Format Floating Point Numbers with std::cout?. For more information, please follow other related articles on the PHP Chinese website!