Nothing has been truncated. You can't truncate a double
; it
always has the same number of bits and the same format. But
you're outputting using the default precision (6) and the
default format, which will always output 6 significant digits,
and no more.
There are two issues here. The first is simple: what format and
precision do you want? There are three possible formats: the
default (which will use either fixed or scientific, depending on
the value, and for which the precision signifies the maximun
number of significant digits), fixed , or scientific (where the
precision signifies the number of digits after the decimal).
Thus, with a precision of 6, depending on the format, your first
value will be:
default: 9292.31
fixed: 9292.312300
scientific: 9.292312E03
For the values you show, fixed would seem most appropriate; but
if you have very small values, it will display less digits, and
for very large values, you can get some very long strings.
The usual way of handling this is to define a manipulator for
whatever the values represent; in this case, I'll just call it
values
, because I don't know anything more precise about your
application, but you should probably find a more meaningful
name:
class value
{
mutable std::ostream* myOwner;
mutable std::ios_base::fmtflags myFlags;
mutable int myPrecision;
void capture( std::ostream& owner )
{
if ( myOwner == NULL ) {
myOwner = &owner;
myFlags = myOwner->fmtflags();
myPrecision= myOwner->precision();
}
}
public:
value() : myOwner( NULL ) {}
~value()
{
if ( myOwner != NULL ) {
myOwner->flags( myFlags );
myOwner->precision( myPrecision );
}
}
friend std::ostream& operator<<( std::ostream& dest, value const& manip )
{
manip.capture( dest );
dest.setf( std::ios_base::fixed, std::ios_base::floatfield );
dest.precision( 6 );
}
};
This will also restore the formatting status at the end of the
full expression (since it should only be used as a temporary);
this is generally a good idea becaues it prevents surprises in
later code. If you're doing any significant output, you'll
create a base class for the save and restore, and derive your
actual manipulator from that.
Once you have this, you can write:
std::cout << value() << a << " " << b << " " << c << std::endl;
All of the values after value()
in the statement will be
output with the desired format.
More generally, do you understand how machine floating point
works? The actual values stored in your doubles will not be
those that you see in the file, because those that you see in
the file are not representable in the usual double format. For
the number of digits you enter, you should not see any
difference if you output the same number of digits, or even one
or two more, but if you output a very large number of digits
(say about 17) you could.