Manipulator | Explanation | Example |
---|---|---|
setw(int) | Set Width - causes the next insertion of a numeric value or a char[] to be inserted in a minimum field width of n. If more than n spaces are required to print the value, then the width is expanded to fit. All values are printed right justified in the field and padded with spaces as to the specified width. If this method is not used, then the default width is 1. After each insertion, the width is reset to the default value. Note that setw() does not work with char or string values. | cout << setw(4) << number << endl; |
setprecision(int) | Set Precision - sets the precision for printing floating point numbers. The precision remains
in effect until changed. The meaning of precision depends upon the format being used.
fixed - precision sets the number of digits after the decimal to display. scientific - precision sets the number of significant digits to use. |
cout << setprecision(4) << number << endl; |
setfill(char) | Set Fill - changes the padding character to the given character. Note that the default padding character is a space. The fill character remains in effect until changed. | cout << setfill('.') << number << endl; |
setiosflags(long) | Set IO Flags - sets the output format flags to n and returns the current settings. The settings
remain until changed. The value of n must be a constant provided in the ios class.The following are
some of the possible values:
ios::fixed - floating point values will be written in fixed decimal format ios::scientific - floating point values will be written in scientific notation i.e. using e notation. ios::left - values will be left justified in the field. ios::right - values will be right justified in the field. |
cout << setiosflags(ios::fixed) << number << endl; cout << setiosflags(ios::scientific) << number << endl; cout << setiosflags(ios::left) << number << endl; cout << setiosflags(ios::right) << number << endl; |
unsetiosflags(long) | Unset IO Flags resets the specified flag and returns the value the flag had prior to being reset. | cout << unsetiosflags(ios::fixed) << number << endl; cout << unsetiosflags(ios::scientific) << number << endl; cout << unsetiosflags(ios::left) << number << endl; cout << unsetiosflags(ios::right) << number << endl; |