Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Detailed description of format function in delphi
Detailed description of format function in delphi
Format ('%*. * f ',[ 10,4,num]);

Parse: Returns the formatted character of the num variable. Integer digits are 10, with 4 digits after decimal point.

For example, num =1234567890.123456, and the processed string is "123457890.6438+0234".

The format function returns characters in the specified format.

Function format (const format: string; Constargs: array of const): string

Specific instructions:

Constfromat: string: format information

Constant parameter: array.

Mainly format information trouble.

Formatted information is mainly composed of the following elements:

" "+[Index ":"]+["-"]+[Width]+["。" Summary]+Type

The functions of formatting information are summarized as follows:

Format ('x=%d', [12]). //'x= 12' // most common

Format ('x=%3d', [12]). //'x= 12' // Specify the width.

Format ('x=%f', [12.0]). //'x= 12.00' // floating point number

Format ('x=%.3f', [12.0]); //'x= 12.000' // Specify a decimal.

Format ('x =% 8.2f' [12.0])//'x =12.00';

Format ('x=%. *f ',[5, 12.0]); //'x= 12.00000' // Dynamic configuration

Format ('x=%.5d', [12]); //'x=000 12' //, starting with 0.

Format ('x=%.5x', [12]); //'x=0000C' // hexadecimal

Format ('x=% 1:d%0:d', [12,13]); //'x= 13 12' // Use index

Format('x=%p ',[nil]); //'x=00000000' // pointer

Format ('x=% 1. 1e', [12.0]); //'x= 1.2E+00 1' // scientific counting method

Format ('x=%%', []); //'x=%' // Get "% "

S := Format('%s%d ',[S,I]); //S:= S+str point(I); //connection string

Detailed explanation of formatted information:

Index ":"] How do you express this? Look at an example.

Format ('This is %d %d', [12,13]);

The index of the first %d is 0, and the index of the second %d is 1, so the characters are displayed as follows: this is 12 13.

If you define it like this:

Format ('This is% 1:d %0:d', [12,13]);

Then the returned string becomes this is 13 12. Do you understand now? The index in [index ":"] indicates the display order of parameters in Args. If this is the case,

Format ('%d %d %d %0:d %d', [1, 2,3,4])

Will return 1 2 3 1 2.

If you want to return to 1 2 3 1 4, you must decide as follows:

Format ('%d %d %d %0:d %3:d', [1, 2,3,4])

However, it should be noted that the index cannot exceed the number in Args, otherwise it will cause an exception, such as

Format ('This is %2:d %0:d', [12,13]);

Since there are only 12 13 numbers in Args, the index can only be 0 or 1, where 2 is wrong. [width] specifies the width of the value to be formatted, which is clear from the example.

Format ('This is %4d', [12]);

The output is: this is 12, which is relatively easy, but if the value of Width is less than the length of the parameter, it has no effect.

For example:

Format ('this is% 1d', [12]);

The output is: This is 12.

["-"] This specified parameter is aligned to the left, and the best effect can be seen when used in combination with [width]:

Format ('This is %-4d, yes', [12]);

The output is: this is 12, that's right.

["."prec] Specify the most suitable precision for floating-point numbers:

Format ('This is %.2f', ['1.1234]);

Output this is 1. 12.

Format ('This is %.7f', ['1.1234]);

Output this is 1. 1234000.

For integers, if the number of digits of prec such as integer is small, it has no effect. On the other hand, if it is greater than the digits of the integer value, the integer value is preceded by 0.

Format ('This is %.7d', [1234]);

The output is: this is 000 1234]

For character type, just the opposite of integer value, if the length of prec is greater than string type, it has no effect, otherwise the tail character will be truncated.

Format ('This is %.2s', ['1234']);

The output is 12, the example mentioned above:

Format ('This is %e', [-2.22]);

What is returned is: This is-2.2200000000000e+000, how to get rid of the extra 0, this will do.

Format ('This is %.2e', [-2.22]);