Usage of formatting functions

xiaoxiao2021-03-06  53

One. Format usage

Format is a very common method, but it seems that I have tried some translators to help this method, let it have a complete profile for everyone to use:

First look at its statement: Function Format (const format: string; string; overload; in fact, the Format method has two forms, and the other is three parameters, the main difference is that it is Thread safety, but not much, so this is only the first introduction:

Function Format (const format: string; const): string; overload; format parameter is a format string for formatting the value in Args. What is args, it is a variant array, that is, there can be multiple parameters in it, and each parameter can be different. As follows: Format ('my name is% 6s', ['wind']); after returning is My Name IS WIND

Now let's see the details of the format parameter: Format can write a normal string, such as 'my name is', but some format instruction characters have special significance, such as "% 6s"

The format instruction has the following form: "%" [index ":"] ["-"] [width] ["." Prec] Type starts with "%", and Type ends, TYPE indicates a specific type. . The intermediate is used to format the Type type instruction character, is optional.

Let's see Type, Type can be the following characters: D 10, indicating that a integer value U and D are integer, but it is no symbol, and if it corresponds to the value, return At a time of 2, a number of absolute values, such as: format ('this is% u', [- 2]); returning: THIS IS 4294967294F corresponds to floating point number E Science representation, corresponding integer Number and floating point, such as Format ('this is% e', [- 2.22]); return: this is -2.2200000000000000e 000, etc. And it will remove the amounts of the value in the value such as Format ('this IS% G', [02.200]); returned: this is 2.2n can only correspond to the floating point, and the value is converted to the number. Looking at an example, I understand Format ('this is% n', [4552.2176]); returning to this is 4,552.22 Note There are two points, one is only two digits after the decimal, wait for this to eliminate this Yes, even if the decimal is not truncated, it will not also be separated by the M coin type as an integer part, but about the currency type has a better format method, here is just simple formatting, it is only a floating Point value format ('this is% m', [9552.21]); Return: This is ¥ 9,552.21p corresponds to the pointer type, the returned value is the address of the pointer, indicating, for example, VAR X: Integer; p: ^ integer; begin x: = 99; p: = @ x; edit1.text: = format ('this is% p', [p]); END; edit1 content is: this is 0012f548s corresponding characters String type, don't say more, x must be a intepier value, return Edit1.Text: = Format ('this is% x', [15]) in hexadecimal form; return is: this isf type After completion, the following is introduced to format Type's instructions: [INDEX ":"] How to express it, see an example format ('this is% D% d', [12, 13]); one of the first% D The index is 0, the second% D is 1, so the character is displayed, this is IS 12 13

And if you define this: Format ('this is% 1: D% 0: D', [12,13]); then the returned string turns this is IS 13 12 now understand, [Index ": "] INDEX indicates the order of parameter display in Args

There is also a situation, if so Format ('% D% D% D% 0: D% D', [1, 2, 3, 4]) will return 1 2 3 1 2. If you want to return 1 2 3 1 4, you must do this: Format ('% D% D% D% 0: D% 3: D', [1, 2, 3, 4]), when used Note 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]); because only 12 13 in the ARGS, INDEX Can only be 0 or 1, here is 2 or wrong [width] Specifies the width of the value that will be formatted, seeing an example, understand Format ('this is% 4d', [12]); output is: This is 12 is relatively easy, but if the value of width is less than the length of the parameter, there is no effect. Such as: format ('this is% 1D', [12]); output is: this is 12 ["-"] This specified parameter is left to Zuo Qi, and [width] is best to see the effect: Format (' This is% -4d, yes ', [12]); output is: this is 12, yes ["." prec] Specify precision, best for floating point: Format (' this is% .2f ', [' 1.1234]); Output this is 1.12 Format ('1.1234]); lost this IS 1.1234000 and for integer, if PREC is small, there is no effect, The number of intersive values ​​is large, and the front of the integer value is 0 to Format ('this is% .7d', [1234]); the output is: this is 0001234] For characters, just, and integer values Conversely, if PREC is not effective if PREC is large, it is not a result than the length of the character string type, the character format of the tail will be cut off the tail, and ['THIS% .2S', ['1234']); output is this IS 12 and this example: Format ('this is% e', [- 2.22]); return: this is -2.2200000000000000e 000 how to get extra 0, this is OK Format ('this is % .2e ', [- 2.22]); Ok, the first one is finished, it should be very familiar with his application.

/// TricatDateTime Usage His Declaration is: Function FormatdateTime (const format: string; datetime: tdatetime): string; Overload; of course, there is one as Format, but here only introduces common first format parameters is one Format string. DateTime is a time type. The return value is a formatted string key to see the command character c in the Format parameter display time in a short time format, that is, all the numbers represent formetime ('c', now); output is: 2004-8- 7 9:55:40d Corresponds to the date, the date is a bit shows one bit, two bits display two formatDatetime ('d', now); output may be the same as 1 ~ 31DD and D, but It is always the formtdatetime ('DD', NOW) that is displayed in two; output may be 01 ~ 31DDD display is the day of the week formatDateTime ('DDD', NOW); the output is the same as the Saturday DDDD and DDD . But the above two may not be the same in other countries. DDDDD Displays the Year Month Day Formator ('DDDD', NOW) in a short-time format; output is: 2004-8-7ddddddd Demonstrate FormatDateTime ('DDDDD', NOW) in long-term format; output is: August 2004 7-day E / EE / EEE / EEEE displays the year formtdatetime ('EE', NOW); output is: 04 (representative 04) m / mm / mmm / mmmm represents Month FormatorTime ('M', NOW ); 8 formatdatetime ('mm', now); output is 08 Formator ('mmm', now); output is August Formator ('mmmm', now); output is the same as DDD / DDDD, In other countries, different YY / YYYY indicate that year formatdatetime ('yy', now); output is 04 Formator ('YYY', NOW); output is 2004H / hh, n / nn, s / s, z / zzz respectively Hours, minute, second, milliseconds Time formatDatetime ('t', now) is displayed in a short time format; output is 10: 17tt display time formatdatetime ('TT', NOW) in long-term format; output is 10:18:46 AMPM Show the morning or afternoon Formator ('ttampm', now) in a long time format; output is: 10: 22: 57 am

To probably, if you want to add a normal string in Format, you can use dual quotes to separate those specific characters, so if special characters are included in normal strings, it will not be displayed as time format: formatdatetime ('" Today IS "C ', NOW); Output is: Today IS 2004-8-7 10:26:58 Can also add" - "or" / "to separate the date: formatdatetime ('" Today IS "YY-mm -dd ', now);' "" Today IS "YY / MM / DD ', NOW); output is: Today IS 04-08-07 can also be separated by": "to Separate time formtime ('" Today IS " HH: NN: SS ', NOW; output is: Today IS 10: 32: 23 / Three. Usage of formatfloat

Commonly used statement: Function formatfloat (value: extended): string; overload; and the same format parameter is formatted by the instruction character, why is the extended type why is this type, because it is in all floating point values The maximum range, if the parameters of the method are incompatible, such as Double or other, you can save not exceeding the range.

The key is to see the usage of the Format parameter 0 This specified directive of the corresponding number of bits. For example: formatfloat ('000.000', 22.22); output is 022.220 note, if the number of 0 of the integer part is less than the number of integers in the value parameter, there is no effect such as formatfloat ('0.00', 22.22); output It is: 22.22 but if the fraction of 0 is less than the multiple of the Value, the corresponding decimal and bit numbers such as: formatfloat ('0.0', 22.22); output: 22.2 can also be specified in integer 0 Commed, this integer number must be greater than 3, will have a comma, formatfloat ('0,000.0', 2222.22); output is: 2,222.2 If this formatfloat ('000, 0.0', 2222.22); its output or: 2,222.2 Its law

# 0 0 usage, I haven't measured any differences yet. Formatfloat ('##. ##', 22.22); output is: 22.00

E science said, seeing several examples, I understand formatfloat ('0.00e 00', 2222.22); output is 2.22E 03 Formatfloat ('0000.00E 00', 2222.22); output is 2222.22e 00 formatfloat ('00.0e 0 ', 2222.22); 22.2e 2 understands, all over the right of E right. This method is not difficult, probably this is like this.

The above three methods are very common, there is nothing skill, just remember these norms. I wrote it, it is convenient for everyone, there is no special purpose.

转载请注明原文地址:https://www.9cbs.com/read-117545.html

New Post(0)