SPRINTF$ (Function)


Converts an expression to a designated format and returns it as a character string.


SPRINTF$ (<Format>, <Expression>)


<Format> includes a character string for output as it is, and a conversion designation character string for converting and outputting the contents of <Expression> (as a part of a character string).
The first character of the conversion designation character string is % and a character (string) follows it.
  • A flag of 0 or more signifies a conversion designation character string (no special order). The minimum field width (option) for writing a character string. If the number of converted character values is less than the minimum field width, the left side (right side if the following left justification flag is designated) of the field is embedded with blanks (default) to fill the minimum field width. The field width value is denoted as an asterisk (*) (described in latter section) or an integer value.
  • Precision (Option). The precision option designates the minimum number of digits displayed in conversion using conversion designation symbols d, i, o, x and X. It designates the number of digits after the decimal point displayed in conversion using conversion designation symbols e, E and f, and the maximum effective number of digits displayed in conversion using conversion designation symbols g and G. It also designates the maximum number of characters written from a character string in conversion using conversion designation symbol s. A period (.) is used as the precision value followed by an asterisk (*) (described in the latter section) or an integer value (option). If only a period is designated, the precision is designated as 0. If precision is designated with other conversion designation symbols, the motion is not defined.
  • Conversion designation symbol
    As just described, an asterisk (*) can be designated for field width and precision. In the case of an integer type argument, designate the value of width and precision. Arguments which designate the field width or precision, or a value of both must be designated before (put field width before precision) an argument (only if this is present) can be converted. If a negative value is designated for the field width, it becomes the same designation as the positive field width designated after a negative conversion designation symbol. If a negative value is designated for precision, it is the same as if the precision is ignored.
    The meanings of the flags are as follows.
    -
    :
    The conversion result is left justified in the field. (If this flag is not designated, it is right justified.)
    +
    :
    In conversion with a sign, the first figure always has a + or - sign (If this flag is not designated, a sign of - is added only for a negative value.)
    <space>
    :
    If <space> and a + flag at the head of the result are designated when the first character is not a sign in conversion with a sign, or when no characters are created after a conversion with a sign, <space> is ignored.
    #
    :
    Conversion is done using the "Replacement format." In conversion with conversion designation symbol o, the precision is changed so that the leading digit of the result becomes 0. In conversion with a conversion designation symbol x (or X), "0x" (or 0X) is added so that the first digit of the result is not 0. In conversion with conversion designation symbols e, E, f, g or G, (even if there are no figures after the decimal point) the decimal point character is always added to the result. (In such conversion, the decimal point character is usually added only when figures are present after the decimal point.) In conversion with conversion designation symbols g and G, the o at the end is not deleted. The operation in other conversion is not defined.
    0
    :
    In conversion with conversion designation symbols d, i, o, x, X, e, E, f, g and G, the minimum field width is ensured by embedding 0 at the head (after a sign or base). There is no embedding with <space>. If a 0 flag and a - flag are designated at the same time, the 0 flag is ignored. If conversion of precision is designated with conversion designation symbols d, i, o, x, and X, a 0 flag is ignored. The operation in cases where other conversion designation symbols are used is not defined.

    The meanings of the conversion designation signs is as follows.
    d, i
    :
    Convert the value of an integer type argument to a character string of decimals with a sign in a "[-] dddd" format. The minimum number of digits displayed is determined by the designated precision. If a converted value does not fill the minimum number of digits, the head is embedded with 0s. The default precision is 1. If a value of 0 is converted with precision 0, no character is output.
    o, x, X
    :
    Convert an integer type argument to a character string of an octal number (o) or hexadecimal number (x or X)without a sign in a "dddd" or hexadecimal number (x or X) format. The lower case letters (abcdef) are used for the conversion designation symbol x and upper case letters (ABCDEF) for the conversion designation symbol X. The minimum number of digits displayed is determined by the designated precision. If a value after conversion does not reach the minimum number of digits, the head is embedded with 0s. The default precision is 1. If a value of 0 is converted with precision 0, no characters are output.
    f
    :
    Converts a real type argument to a character string of decimals displayed in a "[-] ddd.ddd" format. The number of digits after the decimal point is determined by the designated precision. If the precision is not designated, the number of digits after the decimal point is 6. If the precision is 0 and a # flag is not designated, the characters after the decimal point are not displayed. If characters after the decimal point are displayed, at least one digit before the decimal point is displayed. The value is rounded to a proper number of digits.
    e, E
    :
    Convert a real type argument to a character in a "[-] d.ddde+/-dd" format. One digit (other than 0 if the argument is not 0) is displayed before the decimal point character. The number of digits after the decimal point is determined according to the designated precision. If the precision is not designated, the number of digits after the decimal point is 6. If the precision is 0 and a # flag is not designated, the characters after the decimal point are not output. The value is rounded to a proper number of digits. If the conversion designation symbol E is used, the exponent is expressed not with e but E. The exponent is always expressed with 2 digits or more. If the value is 0, the exponential part also becomes 0.
    g, G
    :
    Convert a real type argument to a type of f or e (In the case of G, E is used). The valid number of digits depends on the precision. If the precision is 0, the valid number of digits is 1. The format used is determined by the value to be converted. Conversion of type e (or E) is used only when the exponential part of the conversion result is less than -4, or greater than or equal to the precision. If there is a 0 as the last digit after the decimal point, it is deleted. Figures with a decimal point are displayed only when there is a digit after the decimal point.
    c
    :
    Converts an integer type argument to an ASCII character byte and outputs the characters after conversion.
    S
    :
    System reservation
    %
    :
    Outputs a character of %. The argument is not converted. The designation of complete conversion is %%.

There is no regulation for operation if the conversion designation is wrong.
If the result of conversion is longer than the field width, it may be cut it short but the field is never extended with the conversion result.



S1 = SPRINTF$("% d",123)
'Assigns "123" to S1.
S2 = SPRINTF$("%-6.3f", 1.23)
'Assigns "1.230 " to S2.
S3 = SPRINTF$("%e",123.456#)
'Assigns "1.234560e+002" to S3.
S4 = SPRINTF$("%g",12345678#)
'Assigns "1.23457e+007" to S4.
S5 = SPRINTF$("%#x",128)
'Assigns "0x80" to S5.


Top