About Chandra Archive Proposer Instruments & Calibration Newsletters Data Analysis HelpDesk Calibration Database NASA Archives & Centers Chandra Science Links

Skip the navigation links
Last modified: December 2006

URL: http://cxc.harvard.edu/ciao3.4/sprintfs.tm.html
Hardcopy (PDF): A4 | Letter
AHELP for CIAO 3.4 sprintf Context: slangrtl

Synopsis

Format objects into a string

Syntax

String sprintf (String format, ...);
String_Type Sprintf (String_Type format, ..., Integer_Type n)

Description

Both functions create a string based on the supplied format string and the remaining arguments to the call, in a similar manner to the C function sprintf.

The difference between the two routines is that Sprintf must also be sent the number of items to format (the n parameter above).

The format string is a C library sprintf style format descriptor. Briefly, the format string may consist of ordinary characters (not including the % character), which are copied into the output string as-is, and a conversion specification introduced by the % character. The % character must be followed by at least one other character to specify the conversion:

     s    value is a string
     f    value is a floating point number
     e    print float in exponential form, e.g., 2.345e08
     g    print float as e or g, depending upon its value
     c    value is an ascii character
     %    print the percent character
     d    print a signed decimal integer
     u    print an unsigned decimal integer
     o    print an integer as octal
     X    print an integer as hexadecimal
     S    convert value to a string and format as string

Note that %S is a S-Lang extension which will cause the value to be formatted as string. In fact, sprintf("%S",x) is equivalent to sprintf("%s",string(x)).

Example 1

s = sprintf("%f is greater than %f but %s is better than %s\n", PI, E,
"Cake" "Pie");

Example 2

s = Sprintf("%f is greater than %f but %s is better than %s\n", PI, E,
"Cake" "Pie", 4);

The final argument to Sprintf is the number of items to format; in this case, there are 4 items.

Hardcopy (PDF): A4 | Letter
Last modified: December 2006



The Chandra X-Ray Center (CXC) is operated for NASA by the Smithsonian Astrophysical Observatory.
60 Garden Street, Cambridge, MA 02138 USA.    Email: cxcweb@head.cfa.harvard.edu
Smithsonian Institution, Copyright © 1998-2004. All rights reserved.