函数参考


StringFormat

返回格式化过后的字符 (与 C 语言的sprintf() 函数相似).

StringFormat ( "格式控制", var1 [, ... var32] )

参数

格式控制 使用的格式和标志变量 (见注意)..
var1...var32 最多 32 个代替 "格式控制" 输出的变量.

返回值

返回按照 "格式控制" 参数中定义的 "格式变量" 格式化后的字符串.

注意/说明

为避免缓冲器溢出,每个 "变量" 被限制为 65535 个字符.
转义字符可以包含在 "格式控制" 参数中,例如 \n (@LF), \r (@CR), \t (@TAB]. 因此如果你想输出一个 "\"符号,你需要使用 \\, 同理 "%" 也需要写成 %%.

"格式变量" 为; %[标志] [宽度] [.精确度] 类型

        宽度约定

The second optional field of the format specification is the width specification. The width argument is a nonnegative decimal integer controlling the minimum number of characters printed. 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).

        类型约定


类型 变量类型 输出格式
d, i 整数 十进制带符号整数.
o 整数 八进制无符号整数.
u 整数 十进制无符号整数.
x 整数 十六进制无符号整数, 使用 "abcdef".
X 整数 十六进制无符号整数, 使用 "ABCDEF".
e 浮点 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 浮点 Identical to the e format except that E rather than e introduces the exponent.
f 浮点 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 浮点 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 浮点 Identical to the g format, except that E, rather than e, introduces the exponent (where appropriate).
s 字符串 字符串.

        标志规范


标志 意义 默认值
- 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.

        精度规范

The third optional field of the format specification is the precision specification. It specifies a nonnegative decimal integer, preceded by a period (.), which specifies the number of characters to be printed, the number of decimal places, or the number of significant digits (see Table below). Unlike the width specification, the precision specification can cause either truncation of the output value or rounding of a floating-point value. If precision is specified as 0 and the value to be converted is 0, the result is no characters output, as shown below:

StringFormat( "%.0d", 0 ); /* 没有字符返回 */

    如何确定精度度对格式的影响


类型 意义 默认值
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.

相关

没有.

示例/演示


Local $n = 43951789;
Local $u = -43951789;

; notice the double %%, this prints a literal '%' character
printf("%%d = '%d'\n", $n);             '43951789'          standard integer representation
printf("%%e = '%e'\n", $n);             '4.395179e+007'     scientific notation
printf("%%u = '%u'\n", $n);             '43951789'          unsigned integer representation of a positive integer
printf("%%u <0 = '%u'\n", $u);          '4251015507'        unsigned integer representation of a negative integer
printf("%%f = '%f'\n", $n);             '43951789.000000'   floating point representation
printf("%%.2f = '%.2f'\n", $n);         '43951789.00'       floating point representation 2 digits after the decimal point
printf("%%o = '%o'\n", $n);             '247523255'         octal representation
printf("%%s = '%s'\n", $n);             '43951789'          string representation
printf("%%x = '%x'\n", $n);             '29ea6ad'           hexadecimal representation (lower-case)
printf("%%X = '%X'\n", $n);             '29EA6AD'           hexadecimal representation (upper-case)

printf("%%+d = '%+d'\n", $n);           '+43951789'         sign specifier on a positive integer
printf("%%+d <0= '%+d'\n", $u);         '-43951789'         sign specifier on a negative integer


Local $s = 'monkey';
Local $t = 'many monkeys';

printf("%%s = [%s]\n", $s);             [monkey]            standard string output
printf("%%10s = [%10s]\n", $s);         [    monkey]        right-justification with spaces
printf("%%-10s = [%-10s]\n", $s);       [monkey    ]        left-justification with spaces
printf("%%010s = [%010s]\n", $s);       [0000monkey]        zero-padding works on strings too
printf("%%10.10s = [%10.10s]\n", $t);   [many monke]        left-justification but with a cutoff of 10 characters

printf("%04d-%02d-%02d\n", 2008, 4, 1);

Func Printf($format, $var1, $var2 = -1, $var3 = -1)
    If $var2 = -1 Then
        ConsoleWrite(StringFormat($format, $var1))
    Else
        ConsoleWrite(StringFormat($format, $var1, $var2, $var3))
    EndIf
EndFunc   ;==>Printf