<< Prev        Next >>

ID : 278

Sprintf

Function

To convert data to a specified format and return it as a string.

Syntax

Sprintf(format, data1[,data2[,data3...]])

Guaranteed entry

Format
Designate a conversion format by String Type data.
Data n
Designate data to convert. This can be specified more than one.

Return value

Return string type data.

Description

format can not only assign the arbitrary character string but also can assign the conversion designation string that converts data and returns it as a string.

One Conversion designation string converts one data which is designated as an argument.
If more than one conversion designation strings are entered, arguments are assigned to conversion designation strings according to the order of inputting. The Conversion designation strings convert data of arguments from the top of the sequence.

The example below shows how to specify a conversion designation string.

%[flag][number of digits displayed]conversion designation symbol 

In format, a string with '%' at the top is regarded as the conversion designation string. If you intend to output '%', input as "%%".

  • Conversion designation symbols
    Designation symbols Descriptions
    d, i Signed decimal integer
    o Unsigned octal integer
    x, X Unsigned hexadecimal integer (X is output as a capital letter)
    e, E Floating point number shown in exponential form (E is output as a capital letter)
    f Decimal floating point number
    g, G e or f format (G is output as a capital letter)
    c Character
    s Character strings
  • Flag (option)
    Flag Description
    - Left align a field
    + Output symbol at all time
    Space Output a space instead of symbol if a value is positive number or zero.
    #

    For an output character string, the following process is added.

    • Conversion by "o" adds "0" at the top of the number.
    • Conversion by "x"or "X" add "0x" or "0X" at the top of the number respectively.
    • Conversion by "e", "E", "f", "g"or "G " add decimal point at all time.
    • Conversion by "g" or "G" output zero that are below the decimal point, including unnecessary zero.

    "#" is not effective for other conversion designation symbols.

    0 0' is output if the number of output character is less than the number of digits displayed.

    Zero padding can be specified for numeric values.
    Example Output
    sprintf("[%08.3f]", 123.45678)
    [0123.457]
    sprintf("[%04d]", 1)
    [0001]

    To left-align output, place a minus sign.
    Example Output
    sprintf("[%-16s]", "It is great.")
    [It is great.    ]
    sprintf("[%-8.3f]", 123.45678)
    [123.457 ]
    sprintf("[%-4d]", 1)
    [1   ]
  • Number of digits displayed (option)
    To specify the number of digit displayed, use the format below.
    [Overall number of digits][.accuracy] 

    - Assign decimal number to the overall digit number and the accuracy.
    - The meaning of the "accuracy" differs depending on the designation symbols.
    Designation symbols Descriptions When a designation symbol is not specified (Default value)
    d, i, o, x, X Minimum number of digits Single digit
    e, E, f Number of digits after decimal point Six-digit
    g, G Maximum number of digits Six-digit
    c The precision has no effect. One character
    s Maximum number of characters Number of characters in data

    - If no accuracy value is specified and only a period(.) exists, it is considered that zero has been specified.
    Example Output
    sprintf("[%8.3f]", 123.45678)
    [ 123.457]
    sprintf("[%8.3e]", 1234.5678)
    [1.235e+003]
    sprintf("[%16s]", "It is great.")
    [    It is great.]
    sprintf("[%.8s]", "It is great.")
    [It is gr]

Related Terms

Asc, Chr, Left, Len, Mid, Right, Val, InStr, Bin

Attention

Regardless of the language used, Sprintf uses “.” (a period) as a decimal point, and “,” (a comma) as a thousand separator.
A decimal point and a thousand separator might differ depending on the language.
If you want to use a decimal point and a thousand separator that fit for the language, use Format.

Example

'!TITLE "Convert to specified format and output it as String"
' Convert decimal 200 to a string in the hexadecimal format


Sub Sample_SPrintf


Dim aaa As String
' Convert decimal 200 to a string in the hexadecimal format


aaa = Sprintf( "[%d] in decimal is equivalent to [&H%X] in hexadecimal.", 200, 200 )

'Display "[200] in decimal is equivalent to [&HC8] in hexadecimal." on the message output window
PrintDbg aaa


End Sub

ID : 278

<< Prev        Next >>