Format Format

xiaoxiao2021-03-06  102

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 class, which 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 When it is a number of 32 times to reduce this absolute value? Such as: format ('this is% u', [- 2]) ;? Returning: THIS 4294967294F corresponds to floating point number E Science representation, Corresponding to the integer number and floating point number, such as Format ('this is% e', [- 2.22]);? Returns: this is -2.2200000000000000e 000? Wait a lower note, if the accuracy of the number is reduced to this Only floating-point, and it will remove excess number in the value? For example, Format ('this is% g', [02.200]) ;? Returning: this is 2.2n can only correspond to floating point, will The value is in the form of a number. Look at an example? Format ('this is% n', [4552.2176]);? Returning is this is 4,552.22? Note that there are two points, one is only two digits after the decimal, wait for how to eliminate this What is the case? Second, even if the decimal is not truncated, it will not also be separated by the M coin type as an integer part, but there is a better format method for currency types. Here is just simple formatting? It only corresponds to floating point values? Format ('this is% m', [9552.21]) ;? Returning: this is ¥ 9,552.21p corresponds to the pointer type, the return value is the address of the pointer, in the form of hexadecimal Come, 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 corresponds to a string type, don't say more, X must be a intepier value, return in the form of hexadecimal? Edit1.Text: = Format ('this is% x', [15]) ;? Return is: This is f type tells the completion of the formatting Type: [INDEX ":"] How to express it, see an example?? ????????? format ('this IS% D% D', [12, 13]); ??????????? Where the index of the first% D is 0, Two% D is 1, so when the character is displayed ??????????? This is this IS 12 13

???????????, if you define this: ??????????? Format ('this is% 1: D% 0: D', [12, 13]); ??????????? The returned string becomes ??????????????????????????? now understand? , [Index ":"] in Index indicates the parameter displayed in args ???????????

??????????? There is another 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]) ??????????? But pay attention to it, the index cannot exceed the number in the ARGS, otherwise it will cause an exception?? ?????????? as Format ('this IS% 2: D% 0: D', [12, 13]); ??????????? Due to only 12 13 in Args The number, so INDEX can only be 0 or 1, here is 2 wrong [width] specifies the width of the value that will be formatted, see an example, understand ??????? Format ('this IS % 4d ', [12]); ??????? The output is: this is ?? 12 ??????? This 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, and [width] The effect can be seen together: ?????? Format ('this is% -4d, yes', [12]); ?????? output is: this is 12 ??, YES ??????? ["." prec] specifies the precision, the best effect of floating point: ??????????? Format ('this is% .2f', ['1.1234]); ?????????? Output this is 1.12 ?????????? Format ('this is% .7f', ['1.1234]); ?????????? Losing this is 1.1234000 ?????????? and for the integer, if PREC is smaller, there is no effect ?????????? Anti-than the intelement value The number is large, and it will be in the front of the whole value .????????? format ('this is% .7d', [1234]); ?????????? The output is: this is € 0001234] ????????? ?????????? For the character type, just right and integer, if the prec is larger than the length of the character string type ???? ?????? There is no effect. It is more than the length of the string type, it will cut off the character of the tail ?????????? Format ('this is% .2s', ['1234'] ); ?????????? output is this is 12 ?????????? ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ???? Format ('this is% e', [- 2.22]); ?????????? Returns: this is -2.22000000000000E 000 ?????????? How to get over the extra 0, this is a line ?????????? Format ('this is% .2e', [- 2.22]); ??????????, One is finally finished, it should be very familiar with him.

/// 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 instruction character c in the Format parameter display time in a short time format, that is, all numbers indicate? Formatdatetime ('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 1 ~ 31dd and D Like the meaning, but it is always displayed in two bits? Formatdatetime ('DD', NOW); ?? Output may be displayed in the week ?? formatdatetime ('DDD', now); ?? Output To: Saturday DDDD and DDD display are the same. But the above two may not be the same in other countries. DDDDD DiamatdateTime ('DDDD', NOW) in short time format; ??? Output is: 2004-8-7ddddddddd During the long-time format display year month ??? FormatorTime ('DDDDDD', NOW ); ??? The output is: August 7, 2004 E / EE / EEE / EEEE displays the year ???? formatdatetime ('EE', NOW); ??? Output: 04? Representing 04 years) M / mm / mmm / mmmm represents the month ???? formatdatetime ('m', now); ???? Output is: 8 ???? formatdatetime ('mm', now);??? ? Output? 08 ???? formatdatetime ('mmm', now); ???? output is? August ???? formatdatetime ('mmmm', now); ???? Output? August? ?? Like DDD / DDDD, in other countries, different YY / YYYY indicate the year ???? formatdatetime ('yy', output is 04 ???? formtdatetime ('YYYY', NOW) • ???? The output is 2004H / hh, n / nn, s / s, z / zzz represents hours, divided, second, milliseconds, respectively, show time in a short time format ???? FormatorTDateTime ('t', NOW ); ??? Output is 10: 17tt display time ???? formatdatetime ('tt', now); ???? Output 10:18:46 Ampm Display a morning in the afternoon in a long time format?? ?? formatdatetime ('ttampm', now); ???? The output is: 10: 22: 57 am

To be true, 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, will not be displayed as time format:? Formattime (' "Today IS" C ', NOW);? output is: Today IS 2004-8-7 10:26:58 Time can also add "-" or "/" to separate the date:? formatdatetime (' "Today IS" YY-MM-DD ', NOW) ;? formatdatetime (' "Today IS" YY / MM / DD ', NOW);? output is: Today is 04-08-07 can also be separated by ":" to separate time? Formator ('"Today IS" HH: NN: SS', NOW); Output is: Today IS 10: 32: 23 / Three.Matfloat Usage

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); ?? The output is 022.220 ?? Notice, if the number of integers 0 is less than the number of integers in the value parameter, there is no effect ?? such as: formatfloat '0.00', 22.22); ?? Output: 22.22?? But if the fraction of 0 is less than the multiple of the value of the Value, the corresponding decimal and bit number is case: formatfloat ('0.0', 22.22) ); ?? The output is: 22.2 ?? ?? can also specify a comma in the integer 0, this integer number must be greater than 3, will there be a comma-essay ?? Formatfloat ('0,000.0', 222222); ?? The output is: 2, 222.2 ?? If it is formatfloat ('000, 0.0', 2222.22); ?? It's output or: 2, 222.2? Note its law

Like the usage of #? And 0, I haven't measured what is different. ?? formatfloat ('#####', 22.22); ?? Output is: 22.00

E? Scientific representation, see a few examples probably understand ?? Formatfloat ('0.00e 00', 2222.22); ?? The output is 2.22E 03 ?? Formatfloat ('0000.00E 00', 2222.22); ?? The output is 2222.22e 00 ??? Formatfloat ('00 .0e 0 ', 2222.22); ?? 22.2e 2 ?? understand, all the 0 of the right of E. ?? This method is not difficult, probably this is like this.

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

New Post(0)