When constructing various types of data into strings, the powerful features of sprintf seldom disappoint you. Since sprintf and printf are almost the same in usage, but the printing destination is different, the former is printed
String, and the latter is output directly on the command line. This also makes sprintf much more useful than printf.
One of the most common applications of sprintf is to print Integers to strings. Therefore, spritnf can replace ITOA in most cases.
The sprintf format specifications are as follows. [] Is optional.
% [Specify parameter $] [identifier] [width] [. Precision] indicator
If you want to output '%' itself, handle it like this '%.
Return Value: String
1. process the character direction. -When the negative number is used, the table is processed from the back to the front.
2. Fill in the blanks. If the value is 0, it indicates that the space is filled with 0; if the space is an internal value, it indicates that the space is placed.
3. Total character width. Minimum width.
4. accuracy. The number of digits after the [decimal point.
The integer B is converted into binary.
Convert an integer in C to an ASCII character.
D integer to decimal place.
F converts an exact number to a floating point number.
O integer to octal.
The S integer is converted into a string.
Convert an integer to a lowercase hexadecimal value.
Convert X to uppercase hexadecimal.
$ Money = 123.1
$ Formatted = sprintf ("% 06.2f", $ money); // The variable $ formatted value is "123.10"
$ Formatted = sprintf ("% 08.2f", $ money); // The variable $ formatted value is "00123.10"
$ Formatted = sprintf ("%-08.2f", $ money); // The variable $ formatted value is "123.1000"
$ Formatted = sprintf ("%. 2f %", 0.95*100); // format as a percentage
Sprintf (S, "% 8x", 4567); // lowercase hexadecimal notation, with 8 width positions and right alignment
Sprintf (S, "%-8x", 4568); // in hexadecimal notation, the width occupies 8 positions and is left aligned.
Sprintf (S, "% 08x", 4567); // generate: "201711d7"
The printing and format control of floating point numbers is another common function of sprintf. Floating Point Numbers are controlled by the format character "% F", which is guaranteed by default.
Keep the six digits after the decimal point, for example:
Sprintf (S, "% F", 3.1415926); // generate "3.141593"
But sometimes we want to control the print width and decimal places, then we should use the format "% m. NF", where the M Table
The print width. N indicates the number of digits after the decimal point. For example:
Sprintf (S, "% 10.3f", 3.1415626); // generate: "3.142"
Sprintf (S, "%-10.3f", 3.1415626); // generate: "3.142"
Sprintf (S, "%. 3f", 3.1415626); // The total width is not specified, resulting in: "3.142"
Int I = 100;
Sprintf (S, "%. 2f", I );
What will it do? 100.00 "? Right? Try it on your own, 0.00
Also, try the following:
Sprintf (S, "%. 2f", (double) I );
For more detailed usage, see:
Http://baike.baidu.com/view/1295144.htm
Http://www.kuqin.com/rubycndocument/man/sprintf_format.html#a.c0.ba.c5.d9