Home >Backend Development >C++ >How Can I Precisely Control the Number of Decimal Places When Printing Floating-Point Numbers in C ?
Printing Precision Numbers with C 's I/O Manipulators
In this programming scenario, the goal is to print floating-point numbers with a specific number of decimal places using C 's output manipulator. Let's delve into the solution and examine how it accomplishes this task.
Utilizing the
Here's a practical example to illustrate the usage of these manipulators:
#include <iostream> #include <iomanip> int main() { double d = 122.345; std::cout << std::fixed; std::cout << std::setprecision(2); std::cout << d; }
In this code, std::setprecision(2) sets the output to show two decimal places. The std::fixed manipulator ensures that the number is not displayed in scientific notation. As a result, when the code is executed, it produces the following output:
122.34
This demonstrates how the combination of
The above is the detailed content of How Can I Precisely Control the Number of Decimal Places When Printing Floating-Point Numbers in C ?. For more information, please follow other related articles on the PHP Chinese website!