Home > Article > Backend Development > How can I format floating-point numbers in C using `std::ostream`?
Floating Point Formatting for std::ostream
While attempting to format a double using std::cout, a developer encounters difficulties in achieving the desired output, contemplating resorting to printf_s. They seek a comprehensive source that consolidates formatting options in one place.
The solution involves employing stream manipulators provided by the
<code class="cpp">std::cout << std::fixed << std::setw(11) << std::setprecision(6) << my_double;</code>
This example will print " 42.000000". By adjusting the values passed to std::setw and std::setprecision, you can modify the width and precision of the formatted number. Additionally, std::setfill allows you to specify a fill character for the unoccupied spaces.
The above is the detailed content of How can I format floating-point numbers in C using `std::ostream`?. For more information, please follow other related articles on the PHP Chinese website!