| sprintf |
(String buf, String format [, Object obj1, ..., Object objN]) |
yoix.stdio |
| |
Converts objects to strings under the control of a
format
specification, stores the results in
buf,
which is terminated with a null character,
and returns the number of characters written, or
-1
if there was an error.
The
format
string consists of zero or more of the following components:
-
ordinary characters that are copied directly into
buf.
-
conversion specifications that determine how arguments are converted to strings.
There should be general agreement between the type of
a conversion specification and the corresponding argument.
Each format conversion has the general form,
%[ flag ][ width ][. precision ]specification
where the square brackets indicate optional elements and:
-
flag
is one or more of:
| - |
indicating left-justification.
| | + |
indicating right-justification.
| | (sp) |
indicating right-justify and pad with spaces.
| | 0 |
indicating right-justify and pad with zeros.
| | # |
indicating lead octals with a "0" and hexadecimals with a "0x".
| | ' |
indicating locale specific grouping and decimal point presentation for the default
Locale.
|
-
width
indicates the minimum field width.
-
precision
indicates the maximum field width for strings or, for numbers,
the decimal precision.
-
specification
is one of:
| c |
takes the integer portion of a number and interprets it as a Unicode
character.
| | d |
takes only the integer portion of a number.
| | e or E |
expresses a number using base-10 exponential notation (with a "e" or "E"
being used to indicate where the exponent begins).
| | f |
expresses the real portion of a number using decimal notation.
| | g or G |
provides generic formatting for numbers: for real number, formatting is
like "f" or "e" formatting, as appropriate ("f" or "E" if "G" is specified)
and, for integers, formatting is like d.
| | o |
expresses the integer portion of a number in octal format.
| | O |
formats any object in the manner of the
toString
built-in and uses the
precision,
if supplied, to control the depth of the dump.
| | s or S |
for handling strings ("S" takes the whole string, nulls and all, while "s" stops at the first null).
| | x or X |
expresses the integer portion of a number in hexadecimal format
(with a "x" or "X"
being used to indicate whether the alphabetic digits should be lower-case or
upper-case respectively).
| | % |
just output a percent sign, no argument is converted and the entire
conversion specification must be just "%%".
|
Incidentally, the
strfmt
built-in, which is available in
yoix.string,
is a convenient alternative that provides
sprintf
formatting capabilities without requiring that you supply an output buffer.
| |
| Example: |
The program,
import yoix.stdio.*;
import yoix.system.*;
String buf[] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
sprintf(&buf[10], "%.0f", currentTimeMillis());
printf("%S\n", buf);
prints
XXXXXXXXXX966275730445\0XXXXXXXXXXXXX
on standard output (where
\0
is used to represent the otherwise invisible null character).
| | |
| Return: |
int
| | |
| See Also: |
fprintf,
fscanf,
printf,
scanf,
sscanf,
strfmt,
toString
|
|
Yoix is a registered trademark of AT&T Inc.
|