FAQ cout [Archive] - C Board

PDA

View Full Version : FAQ cout


evilmonkey
10-07-2001, 12:27 PM
Hello,

Could someone please tell me how to format data using cout?

For example, when using printf if I wanted to print out a floating point number to 2 decimal places I would do:

printf("%.2f",num);

Is it possible to do this using cout. The only way I have found is to use sprintf, and give cout the pointer. But surely there is a simpler way.

Thanks in advance.

no-one
10-07-2001, 12:32 PM
cout.precision(3);
cout << 2.3456764 << endl;

should out : 2.34.

keep in mind precision specifies the number of significant digits, not the number of decimal places.