Home >Backend Development >C++ >How Can I Control Decimal Precision When Outputting Floating-Point Numbers in C ?
Preserving Decimal Precision with cout
In this scenario, where we aim to output specific floating-point values while maintaining a constant decimal place count, the setprecision manipulator from the
To achieve this desired formatting, consider the following steps:
For instance, consider the following code snippet:
#include <iostream> #include <iomanip> int main() { double d = 122.345; std::cout << std::fixed; std::cout << std::setprecision(2); std::cout << d; }
This code, when executed, will produce the desired output:
122.34
The above is the detailed content of How Can I Control Decimal Precision When Outputting Floating-Point Numbers in C ?. For more information, please follow other related articles on the PHP Chinese website!