Delphi--格式化字符串

Format strings specify required formats to general-purpose formatting routines.
Description
Format strings passed to the string formatting routines contain two types of objects -- literal characters and format specifiers. Literal characters are copied verbatim to the resulting string. Format specifiers fetch arguments from the argument list and apply formatting to them.
Format specifiers have the following form:
"%" [index ":"] ["-"] [width] ["." prec] type
A format specifier begins with a % character. After the % come the following, in this order:
An optional argument zero-offset index specifier (that is, the first item has index 0), [index ":"]
An optional left justification indicator, ["-"]
An optional width specifier, [width]
An optional precision specifier, ["." prec]
The conversion type character, type
The following table summarizes the possible values for type:
d Decimal. The argument must be an integer value. The value is converted to a string of decimal digits. If the format string contains a precision specifier, it indicates that the resulting string must contain at least the specified number of digits; if the value has less digits, the resulting string is left-padded with zeros.
u Unsigned decimal. Similar to 'd' but no sign is output.
e Scientific. The argument must be a floating-point value. The value is converted to a string of the form "-d.ddd...E+ddd". The resulting string starts with a minus sign if the number is negative. One digit always precedes the decimal point.The total number of digits in the resulting string (including the one before the decimal point) is given by the precision specifier in the format string梐 default precision of 15 is assumed if no precision specifier is present. The "E" exponent character in the resulting string is always followed by a plus or minus sign and at least three digits.
f Fixed. The argument must be a floating-point value. The value is converted to a string of the form "-ddd.ddd...". The resulting string starts with a minus sign if the number is negative.The number of digits after the decimal point is given by the precision specifier in the format string梐 default of 2 decimal digits is assumed if no precision specifier is present.
g General. The argument must be a floating-point value. The value is converted to the shortest possible decimal string using fixed or scientific format. The number of significant digits in the resulting string is given by the precision specifier in the format string梐 default precision of 15 is assumed if no precision specifier is present.Trailing zeros are removed from the resulting string, and a decimal point appears only if necessary. The resulting string uses fixed point format if the number of digits to the left of the decimal point in the value is less than or equal to the specified precision, and if the value is greater than or equal to 0.00001. Otherwise the resulting string uses scientific format.
n Number. The argument must be a floating-point value. The value is converted to a string of the form "-d,ddd,ddd.ddd...". The "n" format corresponds to the "f" format, except that the resulting string contains thousand separators.
m Money. The argument must be a floating-point value. The value is converted to a string that represents a currency amount. The conversion is controlled by the CurrencyString, CurrencyFormat, NegCurrFormat, ThousandSeparator, DecimalSeparator, and CurrencyDecimals global variables or their equivalent in a TFormatSettings data structure. If the format string contains a precision specifier, it overrides the value given by the CurrencyDecimals global variable or its TFormatSettings equivalent.
p Pointer. The argument must be a pointer value. The value is converted to an 8 character string that represents the pointers value in hexadecimal.
s String. The argument must be a character, a string, or a PChar value. The string or character is inserted in place of the format specifier. The precision specifier, if present in the format string, specifies the maximum length of the resulting string. If the argument is a string that is longer than this maximum, the string is truncated.
x Hexadecimal. The argument must be an integer value. The value is converted to a string of hexadecimal digits. If the format string contains a precision specifier, it indicates that the resulting string must contain at least the specified number of digits; if the value has fewer digits, the resulting string is left-padded with zeros.
Conversion characters may be specified in uppercase as well as in lowercase梑oth produce the same results.
For all floating-point formats, the actual characters used as decimal and thousand separators are obtained from the DecimalSeparator and ThousandSeparator global variables or their TFormatSettings equivalent.
Index, width, and precision specifiers can be specified directly using decimal digit string (for example "%10d"), or indirectly using an asterisk character (for example "%*.*f"). When using an asterisk, the next argument in the argument list (which must be an integer value) becomes the value that is actually used. For example,
Delphi example:
Format('%*.*f', [8, 2, 123.456]);
is equivalent to
Format('%8.2f', [123.456]);
C++ example:
TVarRec args[3] = {8,2,123.456};
Format("%*.*f", args, 2);
is equivalent to
TVarRec args[1] = {123.456};
Format("%8.2f", args, 0);
A width specifier sets the minimum field width for a conversion. If the resulting string is shorter than the minimum field width, it is padded with blanks to increase the field width. The default is to right-justify the result by adding blanks in front of the value, but if the format specifier contains a left-justification indicator (a "-" character preceding the width specifier), the result is left-justified by adding blanks after the value.
An index specifier sets the current argument list index to the specified value. The index of the first argument in the argument list is 0. Using index specifiers, it is possible to format the same argument multiple times. For example "Format('%d %d %0:d %1:d', [10, 20])" produces the string '10 20 10 20'.
Note: Setting the index specifier affects all subsequent formatting. That is, Format('%d %d %d %0:d %d', [1, 2, 3, 4]) returns '1 2 3 1 2', not '1 2 3 1 4'. To get the latter result, you have must use  Format('%d %d %d %0:d %3:d', [1, 2, 3, 4])
---------------------------------------------------------------------------------------------
function Format(const Format: string; const Args: array of const): string;
  Format字符串说明:
  "%" [index ":"] ["-"] [width] ["." prec] type
  (1) 格式化字符串必须以%开头
  (2) [index ":"] 索引指的是Args参数列表中要显示的每一项的序号。比如:Args是
                  ['a', 'c'],那么'a'的索引就是0,而'c'的索引就是1,而且由于只有
                  两项,所以就不会出现大于1的索引值。
                  Format('%2:s %1:s %0:s', ['1st', '2nd', '3rd']);
                  结果:'3rd 2nd 1st'
  (3) ["-"] 这个标识符的作用是当要显示的字符的个数少于[width]时,在右边填补空格;
            如果没加上["-"],则在左边填补空格。
            Format('(%4s)', ['aa']); 结果:'  aa'
  (4) [width] 宽度
              规定了要显示的字符的个数。如果要显示的宽度大于[width],则按实际的
              宽度来显示;反之,则填补空格或按要求填补其它字符。
  (5) ["." prec] 精度
                 这是针对浮点数来说的,一般就是指小数点后的位数。
  (6) type 类型(见下面)
  type的可能值有下列这些:
  (1) d 有符号十进制数
        Args必须是有符号整型数。如果在格式化字符串中还加入了["." prec],则如果Args
        的长度如果小于给出的精度数时,在前边填补0;如果大于精度数,按实际长度显示。
        Format('(%.3d)', [99]); 结果:'(099)'
  (2) u 无符号十进制数
        Args必须是无符号整型数。其它特性与d一样。
  (3) e 科学技术法
        用科学技术法显示数据,形式大致如下:'-d.ddd...E+ddd'。
        Args必须是一个浮点数。如果是一个负数,则在最前面显示一个符号;在小数点前面
        总是显示一位数字;包括小数点前面的数字在内,数字的个数由["." prec]来确定,
        如果没有指定["." prec],则默认为15位精度。如果实际的数字长度超出了指定的
        ["." prec],则刚刚超出的那一位数字四舍五入。指数符号E后面总是要跟着加号或
        减号,并且在后面至少跟着三位数字。
  (4) f 固定的
        Args必须为浮点数,转换后的形式大致是'-ddd.ddd...'这样的。
        如果要转换的是负值,则前面有一个负号。转换后的数字,在小数点后面的数字的个数
        由["." prec]决定。如果没有指定["." prec],默认为2位精度。
  (5) g 一般的
        Args必须为浮点数。
        被转换后的数字总是尽可能的简短(有可能是f或e形式的)。有重要意义的数字的长度
        由["." prec]来决定,默认为15位(包括整数位和小数位)。数字前后的0都将被去掉,
        小数点也只有在必要的时候才显示出来。如果小数点左边的数字小于等于指定的精度,
        并且整个值大于或等于0.00001的时候,才使用f的显示格式,否则使用e(科学技术法)
  (6) n Args必须是浮点数。形式和f是一样的,不同的是会显示千位符,如:1,123,444
  (7) m 货币类型
        Args必须是浮点数。能够显示货币符号,可以通过“控制面板”来设置。小数点后
        的位数由["." prec]决定,如果没用["." prec],则默认2位。
  (8) p 指针
        Args必须是一个指针值。
        将指针转换为8个字符的十六进制字符串。
  (9) s 字符串
        Args必须是字符,字符串或PChar值。
        如果指定了["." prec],并且字符串的实际长度大于["." prec],则从左到右截取
        精度指定数量的字符串,其余的删除。
  (10) x 十六进制
         Args必须是一个整型数。
         如果使用了["." prec],不足部分要用0补齐。
  注意:[index ":"] [width] ["." prec]可以使用这样的格式:
        Format('%*.*f', [8, 2, 123.456])
        等价于:Format('%8.2f', [123.456]).
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值