strFormat
Top  Previous  Next

val = strFormat ( string Format [ , Argument1, Argument2 ] … )

Use this function to write formatted data to a string.

Parameters
 
Format  
 
Format control string.  
 
Argument1, Argument2…  
 
Optional arguments.  
 
Return value
 
The string containing formatted data.  
 
Remarks
 
The function formats and stores a series of characters and values in Str. Each Argument (if any) is converted and output according to the corresponding format specification in Format. The Format argument consists of ordinary characters, escape sequences, and (if arguments follow Format) format specifications.  
 
A format specification, which consists of optional and required fields, has the following form:  
%[flags][width][.precision][{h | l | l64 | L}]type  
 
Each field of the format specification is a single character or a number signifying a particular format option. The simplest format specification contains only the percent sign and a type character (for example, %s). If a percent sign is followed by a character that has no meaning as a format field, the character is output to Str. For example, to output a percent-sign character, use %%.  
The optional fields, which appear before the type character, control other aspects of the formatting, as follows:  
 
type  
 
A required character that determines whether the associated argument is interpreted as a character, a string, or a number.  
 
   
Character
Type
Output Format
c
integer
Single-byte character.
d
Integer
Signed decimal integer.
i
Integer
Signed decimal integer.
o
Integer
Unsigned octal integer.
u
Integer
Unsigned decimal integer.
x
Integer
Unsigned hexadecimal integer, using "abcdef."
X
integer
Unsigned hexadecimal integer, using "ABCDEF."
e
floating
Signed value having the form [ – ]d.dddd e [sign]ddd where d is a single decimal digit, dddd is one or more decimal digits, ddd is exactly three decimal digits, and sign is + or –
E
floating
Identical to the e format except that E rather than e introduces the exponent.
f
floating
Signed value having the form [ – ]dddd.dddd, where dddd is one or more decimal digits. The number of digits before the decimal point depends on the magnitude of the number, and the number of digits after the decimal point depends on the requested precision.
G
floating
Signed value printed in f or e format, whichever is more compact for the given value and precision. The e format is used only when the exponent of the value is less than –4 or greater than or equal to the precision argument. Trailing zeros are truncated, and the decimal point appears only if one or more digits follow it.
G
floating
Identical to the g format, except that E, rather than e, introduces the exponent (where appropriate).
s
string
Specifies a character string. Characters are printed up to the first null character or until the precision value is reached.

 
 
flag  
 
An optional character or characters that control justification of output and printing of signs, blanks, decimal points, and octal and hexadecimal prefixes. More than one flag directive may appear in a format specification.  
 
   
Flag
Meaning
Default

Left align the result within the given field width.
Right align.
+
Prefix the output value with a sign (+ or –) if the output value is of a signed type.
Sign appears only for negative signed values (–).
0
If width is prefixed with 0, zeros are added until the minimum width is reached. If 0 and – appear, the 0 is ignored. If 0 is specified with an integer format (i, u, x, X, o, d) the 0 is ignored.
No padding.
blank (' ')
Prefix the output value with a blank if the output value is signed and positive; the blank is ignored if both the blank and + flags appear.
No blank appears.
#
When used with the o, x, or X format, the # flag prefixes any nonzero output value with 0, 0x, or 0X, respectively.
No blank appears.

When used with the e, E, or f format, the # flag forces the output value to contain a decimal point in all cases.
Decimal point appears only if digits follow it.

When used with the g or G format, the # flag forces the output value to contain a decimal point in all cases and prevents the truncation of trailing zeros. Ignored when used with c, d, i, u, or s.
Decimal point appears only if digits follow it. Trailing zeros are truncated.

 
width  
 
An optional number that specifies the minimum number of characters output. If the number of characters in the output value is less than the specified width, blanks are added to the left or the right of the values — depending on whether the – flag (for left alignment) is specified — until the minimum width is reached. If width is prefixed with 0, zeros are added until the minimum width is reached (not useful for left-aligned numbers).  
The width specification never causes a value to be truncated. If the number of characters in the output value is greater than the specified width, or if width is not given, all characters of the value are printed (subject to the precision specification).  
 
precision  
 
An Optional number that specifies the maximum number of characters printed for all or part of the output field, or the minimum number of digits printed for integer values.  
 
   
Type
Meaning
Default
c
The precision has no effect.
Character is printed.
d, i, u, o, x, X
The precision specifies the minimum number of digits to be printed. If the number of digits in the argument is less than precision, the output value is padded on the left with zeros. The value is not truncated when the number of digits exceeds precision.
Default precision is 1.
e, E
The precision specifies the number of digits to be printed after the decimal point. The last printed digit is rounded.
Default precision is 6; if precision is 0 or the period (.) appears without a number following it, no decimal point is printed.
f
The precision value specifies the number of digits after the decimal point. If a decimal point appears, at least one digit appears before it. The value is rounded to the appropriate number of digits.
Default precision is 6; if precision is 0, or if the period (.) appears without a number following it, no decimal point is printed.
g, G
The precision specifies the maximum number of significant digits printed.
Six significant digits are printed, with any trailing zeros truncated.
s
The precision specifies the maximum number of characters to be printed. Characters in excess of precision are not printed.
Characters are printed until a null character is encountered

 

Example
 
I=225  
F=3.14159  
Txt="Format test"  
Str=strFormat("%s: integer %d, floating %5.3f", Txt, I, F)  
Print Str