Detailed description of formatting Functions

Source: Internet
Author: User

Format is a very common but annoying method. I try to translate the help of this method.
To give it a complete overview for your query:

First, let's look at its statement:
Function Format (const format: string; const ARGs: array of const): string; overload;
In fact, the format method has two forms, and the other is three parameters. The main difference is that it is thread-safe,
But it is not widely used, so here we will only introduce the first one:

Function Format (const format: string; const ARGs: array of const): string; overload;
The format parameter is a format string used to format the values in args. What is ARGs,
It is a variant array, that is, it can have multiple parameters, and each parameter can be different.
For example:
Format ('My name is % 6s ', ['wind']);
The returned result is
My name is wind

Now let's take a look at the details of the format parameter:
The format can contain common strings, such as 'My name is'
However, some command characters in the format have special meanings, such as "% 6 s"

The format command has the following forms:
"%" [Index ":"] ["-"] [width] ["." prec] Type
It starts with "%" and ends with type. type indicates a specific type. Is Used in the middle
It is optional to format type instruction characters.

Let's take a look at type. type can contain the following characters:
D in decimal format, indicating an integer value
U is an integer value like D, but it is unsigned. If its value is negative
Is the number of the 32 power minus the absolute value of 2.
For example, format ('this is % U', [-2]);
The returned result is: This is 4294967294
F corresponds to a floating point number
E scientific notation, corresponding to integer and floating point number,
For example, format ('this is % E', [-2.22]);
The returned result is: this is-2.22000000000000e + 000.
It will be explained later if the precision of the number is reduced.
G can only correspond to the floating point type, and it will remove the excess number in the value
For example, format ('this is % G', [02.200]);
The returned result is: This is 2.2
N can only correspond to the floating point type, and the value is converted to the number form. Let's see an example.
Format ('this is % n', [4552.2176]);
This is 4,552.22.
Note that there are two points. One is to indicate only the last two digits of the decimal number. Let's wait and explain how to eliminate this situation.
Second, even if the decimal number is not truncated, it will not be separated by commas like an integer.
M coin type, but there is a better way to format the currency type, here is just a simple format
In addition, it only corresponds to the floating point value.
Format ('this is % m', [1, 9552.21]);
Return Value: This is ¥9,552.21.
P corresponds to the pointer type. The returned value is the pointer address, expressed in hexadecimal format.
For example:
VaR X: integer;
P: ^ integer;
Begin
X: = 99;
P: [email protected];
Edit1.text: = format ('this is % P', [p]);
End;
The content of edit1 is: This is 0012f548
S corresponds to the string type. Needless to say
X must be an integer value, which is returned in hexadecimal format.
Edit1.text: = format ('this is % x', [15]);
The returned result is: This is F.

After the type description is completed, the following describes the instructions for formatting the type:
[Index ":"] How to express this? Let's look at an example.
Format ('this is % d % d', [12, 13]);
The index of the first % d is 0, and the second % d is 1.
Yes. This is 12 13.

If you define it as follows:
Format ('this is % 1: d % 0: d', [12, 13]);
Then the returned string is changed
This is 13 12
Do you understand now? The index in [Index ":"] indicates
Sequence

There is also a case where such format ('% d % 0: d % d', [1, 2, 3, 4])
1 2 3 1 2 is returned.
If you want to return 1 2 3 1 4, you must set it as follows:
Format ('% d % 0: d % 3: d', [1, 2, 3, 4])
Note that the index cannot exceed the number in args. Otherwise, an exception may occur.
For example, format ('this is % 2: d % 0: d', [12, 13]);
Because ARGs has only 12 13 numbers, the index can only be 0 or 1. If it is 2, it will be wrong.
[Width] specifies the width of the value to be formatted. You can see the following example:
Format ('this is % 4D ', [12]);
Output: This is 12
This is relatively easy, but if the width value is smaller than the length of the parameter, there is no effect.
For example, format ('this is % 1D ', [12]);
Output: This is 12
["-"] This specified parameter is aligned to the left, and is combined with [width] to see the effect:
Format ('this is %-4d, yes ', [12]);
Output: This is 12, yes

["." Prec] specifies the precision, which has the best effect on floating point numbers:
Format ('this is %. 2f ', ['1. 1234]);
Output this is 1.12
Format ('this is %. 7f', ['1. 1234]);
This is 1.1234000

For integer data, if the prec such as integer data has a small number of digits, there is no effect.
Otherwise, it is greater than the number of digits of the integer value.
Format ('this is %. 7D ', [1234]);
Output: This is 0001234]

For the character type, it is just opposite to the integer value. If the prec type is longer than the string type
It does not work. If the string type is smaller than the string type, the trailing characters are truncated.
Format ('this is %. 2s ', ['123']);
The output is this is 12.

The example above is as follows:
Format ('this is % E', [-2.22]);
The returned result is: this is-2.22000000000000e + 000.
How can we remove the excess 0?
Format ('this is %. 2e', [-2.22]);

Okay. The first one is finished. I should be familiar with his application.

//////////////////////////////////////// ///////////////////////
Ii. formatdatetime usage
His statement is:
Function formatdatetime (const format: string; datetime: tdatetime): string;
Overload;
Of course, there is another one like format, but here we only introduce the first commonly used
The format parameter is a formatted string. Datetime is a time type. The returned value is formatted.
String

Focus on the instruction characters in the format parameter.
C. display the time in a short time format, that is, all numbers.
Formatdatetime ('C', now );
Output: 9:55:40
D corresponds to the date in the time. If the date is one digit, one digit is displayed, and two digits are displayed.
Formatdatetime ('D', now );
The output may be 1 ~ 31
Dd AND d have the same meaning, but it is always displayed in two places.
Formatdatetime ('dd', now );
The output may be from 01 to 01 ~ 31
Ddd displays the day of the week.
Formatdatetime ('ddd ', now );
Output: Saturday
Dddd and DDD are displayed the same.
However, the two above may be different in other countries.
Ddddd displays year, month, and day in a short time format
Formatdatetime ('ddddd ', now );
Output: 2004-8-7
Dddddd displays year, month, and day in long time format
Formatdatetime ('dddddddd', now );
Output: January 1, August 7, 2004
E/EE/EEE/eeee shows the year in the corresponding number of digits
Formatdatetime ('ee ', now );
Output: 04 (Year 04)
M/MM/Mmm/mmmm indicates month
Formatdatetime ('M', now );
Output: 8
Formatdatetime ('mm', now );
Output is 08
Formatdatetime ('mmm', now );
Output: January 1, August
Formatdatetime ('mmmm', now );
Output: January 1, August
Like DDD/dddd, it may be different in other countries.
YY/YYYY indicates year
Formatdatetime ('yy', now );
Output: 04
Formatdatetime ('yyyy', now );
Output is 2004
H/HH, N/nn, S/SS, and z/ZZZ indicate hours, minutes, seconds, and milliseconds respectively.
T display time in short time format
Formatdatetime ('T', now );
Output:
TT display time in long time format
Formatdatetime ('TT', now );
The output is 10:18:46.
Whether the ampm is displayed in the morning or afternoon in a long time format
Formatdatetime ('ttampm ', now );
Output: 10: 22: 57 AM

In this case, if you want to add a common string in format, you can use double quotation marks to separate
Special characters, so that if a common string contains special characters, it will not be displayed
Time Format:
Formatdatetime ('"today is" C', now );
Output: Today is 10:26:58
You can also add "-" or "\" to separate the date:
Formatdatetime ('"today is" YY-mm-dd', now );
Formatdatetime ('"today is" YY \ mm \ dd', now );
Output: Today is 04-08-07
You can also use ":" to separate the time
Formatdatetime ('"today is" HH: NN: ss', now );
Output: Today is 10:32:23

//////////////////////////////////////// /////////////////////////
Iii. formatfloat usage

Common statements:
Function formatfloat (const format: string; Value: Extended): string; overload;
The format parameter is the formatting command character, and the value is of the extended type.
Why is this type, because it indicates the maximum range among all floating point values, if the parameter of this method is input
For example, if it is double or other, it can be saved without exceeding the range.

The key is to check the format parameter usage.
0.
For example: formatfloat ('000. 000', 22.22 );
The output is 022.220.
Note that if the number of zeros in the integer part is smaller than the number of digits in the value parameter, the result is ineffective.
For example, formatfloat ('0. 00', 22.22 );
Output: 22.22
However, if the decimal part 0 is less than a multiple of the small numbers in the value, the corresponding decimal places and digits are truncated.
For example, formatfloat ('0. 0', 22.22 );
Output: 22.2

You can also specify a comma in integer 0. The number of digits must be greater than three to produce a comma.
Formatfloat ('20140901', 0,000.0 );
Output: 2,222.2
If formatfloat ('000, 100', 0.0 );
Its output is still: 2,222.2
Pay attention to its laws

# The usage is the same as that of 0. Currently, I have not figured out any difference.
Formatfloat ('##. ##', 22.22 );
Output: 22.00

E. Scientific notation. Let's see a few examples.
Formatfloat ('0. 00e + 00', 2222.22 );
The output is 2.22e + 03.
Formatfloat ('2014. 00e + 00', 0000 );
The output is 2222.22e + 00.
Formatfloat ('00. 0e + 0', 2222.22 );
22.2e + 2
Do you understand? It depends on 0 on the Right of E.

This method is not difficult. It is probably like this.

The above three methods are very common and there is no skill. Just remember these specifications.

Detailed description of formatting Functions

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.