Home / Cplusplus / Formatting In C++

Formatting In C++

When a program inserts or extracts values other than single characters, such as integers or floating-point data, a number of different formatting options are available.
 
For instance, some applications might want to have an inserted unsigned int transmitted in decimal, while for other applications hexadecimal might be more appropriate.
 
A similar issue is whether white space should be skipped on input before storing or interpreting characters from a stream.
 
The member function setf() is provided to allow program control of such options.
 
For instance, the following expression sets the default for the stream cout to hexadecimal, so that integral values written to cout will ordinarily be transmitted in hexadecimal:
 
cout.setf(ios:hex, ios:basefield)
 
The streams library provides several similar functions:
 
width()
 
sets the number of characters to display.
 
fill()
 
defines the fill character when there are fewer characters to insert than the width.
 
precision()
 
sets the number of significant digits to write for floating-point values