C ++ Study Notes 1 (extended: Format control in C ++), extended learning notes
In the previous chapter, we learned about the standard input and output problems in C ++, so some people may ask questions. In the C language, we can flexibly control the output and display, can I implement it in C ++? My answer is certainly yes, but the control in C ++ may be a little more complicated than that in C ++, then let's take a look at the format control in the most C ++ (all of the following information comes from the http://www.cplusplus.com ):
1. How to set the base:
Decimal number equivalent to % d
Hex hexadecimal number equivalent to % x
Oct octal number equivalent to % o
1 #include <iostream> 2 3 using namespace std; 4 5 int main(int argc, char **argv) 6 { 7 int val = 100; 8 cout << "dec: " << dec << val << endl; 9 cout << "hex: " << hex << val << endl;10 cout << "oct: " << oct << val << endl;11 return 0;12 }
View Code
Output result:
dec: 100hex: 64oct: 144
2. Floating Point format Control
Set setprecision (n) To show n decimal places
Setiosflags (ios: fixed) fixed floating point display
Setiosflags (ios: scientific) index Representation
Setiosflags (ios: left) left alignment
Setiosflags (ios: right) right-aligned
1 # include <iostream> 2 using namespace std; 3 # include <iomanip> 4 5 int main (int argc, char ** argv) 6 {7 double dv= 123456.654321; 8 9 cout <"Default dv output:" <dv <endl; 10 cout <"dv is specified as a floating point output:" <setiosflags (ios: fixed) 11 <dv <endl; 12 cout <"dv is specified as a floating point output with two decimal places:" <setiosflags (ios: fixed) 13 <setprecision (2) <dv <endl; 14 cout <"dv is specified as an exponential output (5 decimal places are reserved by default):" <setiosflags (ios:: scientific) 15 <dv <endl; 16 cout <"dv is specified as an exponential output with 10 decimal places:" <setiosflags (ios: scientific) 17 <setprecision (10) <dv <endl; 18 cout <setiosflags (ios: left) <setprecision (5) <dv <endl; 19 cout <setiosflags (ios: right) <setprecision (6) <dv <endl; 20 return 0; 21}
View Code
Output result:
1 dv default output: 1234572 dv is set to floating point output: 123456.6543213 dv is set to floating point output, and two decimal places are retained: 123456.654 dv is set to exponential output (5 decimal places are reserved by default ): 1.2e + 055 dv is specified as exponential output, and 10 decimal places are retained: 123456.65436 1.2346e + 057 123457
View Code
Setw (n) sets the domain width to n characters
Setfill (char); set the padding character
1 # include <iostream> 2 using namespace std; 3 # include <iomanip> 4 5 int main (int argc, char ** argv) 6 {7 int val = 100; 8 double pi = 3.14; 9 10 // The output width is 10 and left aligned. Fill in '*' 11 cout if it is not enough <"val:" <setw (10) <setiosflags (ios: left) 12 <setfill ('*') <val <endl; 13 // The output width is 10 and the right alignment is displayed, fill in '* '14 cout <"pi:" <setw (10) <setiosflags (ios: right) 15 <setfill ('*') <pi <endl; 16 return 0; 17}
View Code
Let's talk about the format control in C ++. Of course, there are many format control methods in addition to the above mentioned. The details are to be found.