Delphi Format String Function

Source: Internet
Author: User

VaR
S: string;
Begin
// Command type

S: = format ('maximum INTEGER: % d; minimum integer: % d', [maxint, low (integer)]);
// Return value: the maximum integer is 2147483647, and the minimum integer is-2147483648.
{Note: The format command must start with %, which is case-insensitive. % d indicates an integer. The second parameter is a variant array}

S: = format ('the maximum unnegative integer is: % U', [high (Cardinal)]);
// Return: The maximum unnegative integer is 4294967295.
{% Utable shows a non-negative integer}

S: = format ('input-2 result: % U', [-2]);
// Return: the result of input-2 is 4294967294.
{If % u is a negative number, return: maximum value of an integer without a negative number-absolute value of this number + 1}

S: = format ('% s! % S', [' ',' I am in case']);
// Return: Hello! I am in case
{% S indicates a string}

S: = format ('% F', [Pi]);
// Return value: pI value: 3.14
{% F indicates a floating point number, which is reserved or can contain two decimal places}

S: = format ('% G', [1, 01.2345000]);
// Return value: 1.2345
{% G indicates a floating point number, and the excess 0 is removed}

S: = format ('% n', [12345.6789]);
// Return value: 12,345.68
{% N indicates a floating-point number. The integer part uses the thousands separator and retains two decimal places}

S: = format ('% m', [1, 12345.6789]);
// Return value: ¥12,345.68
{% M indicates a floating point number and a currency mark. The conversion result depends on the region settings of the system}

S: = format ('% E', [1, 12345.6789]);
// Return value: 1.23456789000000e + 004
{% E use scientific notation to represent an integer or floating point number}

S: = format ('% P', [@ self]);
// Return value: 0012f5bc
{% P indicates the pointer address, expressed in hexadecimal format}

S: = format ('% x', [1, 255]);
// Return: FF
{% X represents an integer in hexadecimal notation}

// Index

S: = format ('% S % s', ['wan', '1']);
S: = format ('% 0: S % 1: s', ['wan', '1']);
// Return: In case
{The results of the above two rows are the same. 0: corresponds to the first value in the array; 1: corresponds to the second value in the array}

S: = format ('% 1: S % 0: s', ['wan', '1']);
// Return value: 10 thousand
{Flipped order}

S: = format ('% 1: S % 0: S % 0: S % 1: s', ['wan', 'yi']);
// Return: 10 thousand in case
{Repeated use}

// Align width with alignment

S: = format ('% d, % 8D, % d', [1, 2, 3]);
// Return value: 1, 2, 3
{An 8-character width is specified for the second value. The missing value is filled with spaces. If the second value is missing, the parameter is invalid}

S: = format ('% d, %-8D, % d', [1, 2, 3]);
// Return value: 1, 2, 3
{-Indicates left alignment. The default value is right alignment.-The symbol is before width and behind index}

// Specify the precision of prec

S: = format ('%. 9f', [Pi]);
// Return value: 3.141592654
{Specify the number of digits to the decimal point. value range: 1-9. Enter 0 and use 1}

S: = format ('%. 5D', [12]);
// Return value: 00012
{This is the number of digits specified for the integer. If it is small, it is invalid}

S: = format ('%. 3s', ['format']);
// Return:
{Specify the number of digits for the string, which is invalid if it is large}

S: = format ('%. 3e', [1, 123.456]);
// Return value: 1.23e + 002
{Specify the number of digits for the scientific notation}

// Command order:

{"%" [Index ":"] ["-"] [width] ["." prec] type}

Showmessage (s );
End;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.