sprintf

The sprintf function is the same as printf except that it does not print the result of the conversion but returns it as a string. Of course, if the trailing semicolon at end of the sprintf command is not present, then the returned string is still printed.
>> s = sprintf("e = %g, pi = %g\n", e, pi);
>> s
   e = 2.71828, pi = 3.14159

Normal characters contained in the format string are printed literally, such as the strings e = and pi = and \n. Each substring in the format that starts with the character % and ends with a conversion character is called a conversion specification. Valid conversion characters include d, f, g, etc.. For example, in the format k = %5d, e = %g, pi = %g\n, %5d and %g are conversion specifications, and the rest are normal characters.

Between the % sign and the type character there may be, in order,

The following is a list of the conversions that the functions of the printf family can perform.


Character Type of Value
d,i integer of signed decimal format
o integer of unsigned octal format
x,X integer of unsigned hexadecimal format
u integer of unsigned decimal format
c ASCII character
s character string
f floating point number of double precision, decimal notation
e,E floating point number of double precision, scientific notation
g,G floating point number of double precision; scientific notation if size of exponent is too big, decimal notation otherwise.
% print a %

oz 2009-12-22