Specify different field widths. To show the width for each output, use the | character. By
default, the output text is padded with space characters when the field width is greater
than the number of characters.
txt = sprintf('|%e|%15e|%f|%15f|', pi50ones(1,4))
txt =
'|1.570796e+02| 1.570796e+02|157.079633| 157.079633|'
When used on text input, the field width can determine whether to pad the output text
with spaces. If the field width is less than or equal to the number of characters in the
input text, then it has no effect.
txt = sprintf('%30s', 'Pad left with spaces')
txt =
' Pad left with spaces'
Flags
Optional flags control additional formatting of the output text. The table describes the
characters you can use as flags.
Character Description Example
Minus sign (-) Left-justify the converted
argument in its field.
%-5.2d
Plus sign (+) For numeric values, always
print a leading sign character
(+ or -).
For text values, right-justify the
converted argument in its field.
%+5.2d
%+5s
Space Insert a space before the value.% 5.2f
Zero ( 0 ) Pad with zeroes rather than
spaces.
%05.2f
Formatting Text