
Originally Posted by
marcel
Actually the link says something different:
- On the default floating-point notation, the precision field specifies the maximum number of meaningful digits to display in total counting both those before and those after the decimal point. Notice that it is not a minimum and therefore it does not pad the displayed number with trailing zeros if the number can be displayed with less digits than the precision.
- In both the fixed and scientific notations, the precision field specifies exactly how many digits to display after the decimal point, even if this includes trailing decimal zeros. The number of digits before the decimal point does not matter in this case.
So whether the number is a float or a double doesn't have any impact on the stream.
Here's a small example:
#include <iostream>
#include <iomanip>
int main( int argc, char **argv )
{
double d1 = 1.23456789;
double d2 = 12345.6789;
float f1 = 1.23456789;
float f2 = 12345.6789;
std::cout << std::setprecision( 7 );
std::cout << d1 << std::endl;
std::cout << d2 << std::endl;
std::cout << f1 << std::endl;
std::cout << f2 << std::endl;
std::cout << std::fixed;
std::cout << d1 << std::endl;
std::cout << d2 << std::endl;
std::cout << f1 << std::endl;
std::cout << f2 << std::endl;
return 0;
}
#include <iostream>
#include <iomanip>
int main( int argc, char **argv )
{
double d1 = 1.23456789;
double d2 = 12345.6789;
float f1 = 1.23456789;
float f2 = 12345.6789;
std::cout << std::setprecision( 7 );
std::cout << d1 << std::endl;
std::cout << d2 << std::endl;
std::cout << f1 << std::endl;
std::cout << f2 << std::endl;
std::cout << std::fixed;
std::cout << d1 << std::endl;
std::cout << d2 << std::endl;
std::cout << f1 << std::endl;
std::cout << f2 << std::endl;
return 0;
}
To copy to clipboard, switch view to plain text mode
And the output:
$ ./a.out
1.234568
12345.68
1.234568
12345.68
1.2345679
12345.6789000
1.2345679
12345.6787109
As you can see, if no notation was set, "precision" means the number of meaningful digits, regardless whether we print a double or a float. After you set notation to fixed, "precision" behaves as one would expect.
Bookmarks