smanip setprecision ( int n )和ios_base& fixed ( ios_base& str )配合可以按照精度输出浮点值。
输出流默认的精度为6位。
#include <iostream>
#include <iomanip>
int main()
{
double number = 12.34;
std::cout << std::fixed << number << std::endl;
std::cout << std::setprecision(0) << number << std::endl;
std::cout << std::setprecision(1) << number << std::endl;
std::cout << std::setprecision(2) << number << std::endl;
std::cout << std::setprecision(3) << number << std::endl;
std::cout << std::setprecision(4) << number << std::endl;
std::cout << std::setprecision(5) << number << std::endl;
std::cout << std::fixed << std::setprecision(5) << number << std::endl;
system("pause");
return 0;
}
12.340000
12
12.3
12.34
12.340
12.3400
12.34000
12.34000