C setprecision()
The setprecision()
function in C is used to set the precision of floating-point numbers when they are printed. It is part of the <iomanip>
library in C++ and is used in conjunction with the std::cout
object to control the precision of floating-point output.
Here is an example of how to use the setprecision()
function:
#include <iostream>
#include <iomanip>
int main() {
double number = 3.14159;
std::cout << std::setprecision(4) << number << std::endl;
return 0;
}
In this example, the setprecision(4)
function is used to set the precision of the output to 4 decimal places. The number
variable is then printed using std::cout
, and the output will be 3.1416
.
Please note that the setprecision()
function only affects the precision of the output and does not round the actual value of the floating-point number.