Special Character
Representation in Format
Specifier
Alarm
\a
Backspace
\b
Form feed
\f
New line
\n
Carriage return
\r
Horizontal tab
\t
Vertical tab
\v
Character whose Unicode numeric value can be represented by
the hexadecimal number, N
\xN
Example: sprintf('\x5A')
returns 'Z'
Character whose Unicode numeric value can be represented by
the octal number, N
\N
Example: sprintf('\132')
returns 'Z'
Setting Field Width and Precision
The formatting operator follows a set of rules for formatting output text to the specified field width
and precision. You also can specify values for the field width and precision outside the format
specifier, and use numbered identifiers with the field width and precision.
Rules for Formatting Precision and Field Width
The figure illustrates how the field width and precision settings affect the output of the formatting
functions. In this figure, the zero following the % sign in the formatting operator means to add leading
zeroes to the output text rather than space characters.
• If the precision is not specified, then it defaults to six.
• If the precision p is less than the number of digits in the fractional part of the input, then only p
digits are shown after the decimal point. The fractional value is rounded in the output.
• If the precision p is greater than the number of digits f in the fractional part of the input, then p
digits are shown after the decimal point. The fractional part is extended to the right with p-f
zeroes in the output.
• If the field width is not specified, then it defaults to p+1+n, where n is the number of digits in the
whole part of the input value.
6 Characters and Strings
6-28
Précédent

- 210/1450

Suivant