C++ examples for Language Basics:Console
Manipulator | Action |
---|---|
setw(n) | Set the field width to n. |
setprecision(n) | Set the floating-point precision to n places. |
setfill('x') | Set the default leading fill character to x. |
setiosflags(flags) | Set the format flags. |
scientific | Set the output to display real numbers in scientific notation. |
showbase | Display the base used for numbers. A leading 0 is displayed for octal numbers and a leading 0x for hexadecimal numbers. |
showpoint | Always display six digits total. Fill with trailing zeros, if necessary. |
showpos | Display all positive numbers with a leading + sign. |
boolalpha | Display Boolean values as true and false rather than 1 and 0. |
dec | Set the output for decimal display, which is the default. |
endl | Output a newline character and display all characters in the buffer. |
fixed | Always show a decimal point and use a default of six digits after the decimal point. Fill with trailing zeros, if necessary. |
flush | Display all characters in the buffer. |
left | Left-justify all numbers. |
hex | Set the output for hexadecimal display. |
oct | Set the output for octal display. |
uppercase | Display hexadecimal digits and the exponent in scientific notation in uppercase. |
right | Right-justify all numbers (the default). |
noboolalpha | Display Boolean values as 1 and 0 rather than true and false. |
noshowbase | Don't display octal numbers with a leading 0 and hexadecimal numbers with a leading 0x. |
noshowpoint | Don't use a decimal point for real numbers with no fractional parts, don't display trailing zeros in the fractional part of a number, and display a maximum of six decimal digits only. |
noshowpos | Don't display leading + signs (the default). |
nouppercase | Display hexadecimal digits and the exponent in scientific notation in lowercase. |
The following code shows the use of this field width.
#include <iostream> #include <iomanip> using namespace std; int main()/*from w w w . ja v a 2 s . c o m*/ { cout << setw(3) << 6 << endl << setw(3) << 18 << endl << setw(3) << 124 << endl << "---\n" << (6+18+124) << endl; cout << "The number is " << setw(6) << setiosflags(ios::fixed) << setprecision(2) << 26.27 << endl; cout << "The number is " << setw(6) << setiosflags(ios::fixed) << setprecision(2) << 12.3 << endl; cout << "The number is " << setw(6) << setiosflags(ios::fixed) << setprecision(2) << 1.12 << endl; cout << setw(6) << setiosflags(ios::fixed) << setprecision(2) << 12.27 << endl; cout << setw(6) << setiosflags(ios::fixed) << setprecision(2) << 122.3 << endl; cout << setw(6) << setiosflags(ios::fixed) << setprecision(2) << 1.1212 << endl; cout << "------\n"; cout << setw(6) << setiosflags(ios::fixed) << setprecision(2) << 26.27 + 12.3 + 1.1212 << endl; cout << setw(5) << setiosflags(ios::fixed) << setprecision(2) << 26.27 << endl; cout << setw(5) << setiosflags(ios::fixed) << setprecision(2) << 1212.3 << endl; cout << setw(5) << setiosflags(ios::fixed) << setprecision(2) << 1.1212 << endl; cout << "-----\n"; cout << setw(5) << setiosflags(ios::fixed) << setprecision(2) << 26.27 + 1212.3 + 1.1212 << endl; cout << setw(5) << setiosflags(ios::fixed) << setprecision(2) << 36.12 << endl; cout << setw(5) << setiosflags(ios::fixed) << setprecision(2) << 10.1212 << endl; cout << "-----" << endl; cout << "\nThe value of 14 in octal is " << oct << 14 ; cout << "\nThe value of 14 in hexadecimal is " << hex << 14 ; cout << "\nThe value of 0xA in decimal is " << dec << 0xA ; cout << "\nThe value of 0xA in octal is " << oct << 0xA ; cout << endl; return 0; }