Specifier
Description
d
Decimal notation (signed).
e
Exponential notation (using a lowercase e, as in 3.1415e+00).
E
Exponential notation (using an uppercase E, as in 3.1415E+00).
f
Fixed-point notation.
g
The more compact of %e or %f. (Insignificant zeroes do not print.)
G
Same as %g, but using an uppercase E.
o
Octal notation (unsigned).
s
Character vector or string array.
u
Decimal notation (unsigned).
x
Hexadecimal notation (unsigned, using lowercase letters a–f).
X
Hexadecimal notation (unsigned, using uppercase letters A–F).
For example, format the number 46 using different conversion characters to display the number in
decimal, fixed-point, exponential, and hexadecimal formats.
A = 46*ones(1,4);
txt = sprintf('%d
%f
%e
%X', A)
txt =
'46
46.000000
4.600000e+01
2E'
Subtype
The subtype field is a single alphabetic character that immediately precedes the conversion
character. Without the subtype field, the conversion characters %o, %x, %X, and %u treat input data as
integers. To treat input data as floating-point values instead and convert them to octal, decimal, or
hexadecimal representations, use one of following subtype specifiers.
b
The input data are double-precision floating-point values rather than unsigned integers. For
example, to print a double-precision value in hexadecimal, use a format like %bx.
t
The input data are single-precision floating-point values rather than unsigned integers.
Precision
The precision field in a formatting operator is a nonnegative integer that immediately follows a
period. For example, in the operator %7.3f, the precision is 3. For the %g operator, the precision
indicates the number of significant digits to display. For the %f, %e, and %E operators, the precision
indicates how many digits to display to the right of the decimal point.
Display numbers to different precisions using the precision field.
txt = sprintf('%g
%.2g
%f
%.2f', pi*50*ones(1,4))
txt =
'157.08
1.6e+02
157.079633
157.08'
While you can specify the precision in a formatting operator for input text (for example, in the %s
operator), there is usually no reason to do so. If you specify the precision as p, and p is less than the
number of characters in the input text, then the output contains only the first p characters.
Formatting Text
6-25
Précédent

- 207/1450

Suivant