A detailed description of format and FormatDateTime functions in Delphi

Source: Internet
Author: User
Tags integer numbers truncated

Format is a very common, but seems very annoying method, I try to help with this method of some translation, let it have a complete overview, for everyone to query for:

First look at its statement:
function Format (const format:string; const Args:array of const): string; overload;

In fact, the Format method has two forms, the other is three parameters, the main difference is that it is thread-safe,
But not much, so here's only the first introduction:
function Format (const format:string; const Args:array of const): string; overload;

The format parameter is a formatted string that is used to format the value inside args. What is args?
It is a Variant array, that is, it can have multiple parameters, and each parameter can be different.
As in the following example:

Format (' My name is%6s ', [' wind ']);
Back to my name is wind

Now look at the details of the format parameter:
Format can be used to write ordinary strings, such as ' My name is ', but some format instruction characters have special meaning, such as "%6s" format directives have the following form:
"%" [index ":"] ["-"] [width] ["." Prec] Type
It starts with "%" and ends with type, and type represents a specific type. The middle is used to
The format type of the instruction character, which is optional.

First look at Type,type can be the following characters:
d 10 number, representing an integer value
U and D are integer values, but it is unsigned, and if the corresponding value is negative, then the return is a 2 of the 32 minus the absolute number, such as:
Format (' This is%u ', [-2]);
Returned: This is 4294967294

f corresponding floating-point number
e scientific notation, corresponding to integers and floating-point numbers, such as
Format (' This is%e ', [-2.22]);
The return is: This is-2.22000000000000e+000, wait a minute again explain if the precision of the number is reduced

G This can only correspond to floating-point type, and it will remove the extra number of values, such as
Format (' This is%g ', [02.200]);
Returned: This is 2.2

n can only correspond to floating point type, the value is converted into the form of a number. Look at an example and you'll see.
Format (' This is%n ', [4552.2176]);
Returns the IS 4,552.22

Note that there are two points, one is only expressed to two decimal places, wait a moment to say how to eliminate this situation, and two, even if the decimal is not truncated, it will not be like an integer part of the same comma to separate

M coin type, but there is a better format method for currency type, here is simply formatting, and it only corresponds to floating point value
Format (' This is%m ', [9552.21]);
return: This is¥9,552.21

P corresponds to the pointer type, and the returned value is the address of the pointer, expressed in 16-binary form
For example:
var X:integer;
P:^integer;
Begin
x:=99;
P:[email protected];
Edit1.text:=format (' This is%p ', [P]);
End
The contents of Edit1 are: this is 0012f548

s corresponds to a string type, no more talking.
x must be an integer value, returned in 16-binary form
Edit1.text:=format (' This is%x ', [15]);
Return yes: this is F

Type is complete, instructions for formatting the type are described below:
[index]: How to express this, see an example
Format (' This is%d%d ', [12,13]);
Where the index of the first%d is 0, the second%d is 1, so the character is displayed when this is 12 13

And if you define this:
Format (' This is%1:d%0:d ', [12,13]);
Then the returned string becomes this is 13 12. Now do you understand that index in [index:] indicates the order in which the parameters are displayed in args.
Format ('%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, this must be the case:
Format ('%d%d%d%0:d%3:d ', [1, 2, 3, 4])

However, it is important to note that the index cannot exceed the number of args, otherwise it will cause an exception such as
Format (' This is%2:d%0:d ', [12,13]);
Since there are only 12 132 numbers in args, index can only be 0 or 1, where 2 is wrong [width] Specifies the width of the value to be formatted, see an example to see

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, then there is no effect.
Such as:

Format (' This is%1d ', [12]);
Output yes: This is 12

["-"] This specified parameter is left-aligned, and [width] together to see the effect:
Format (' This is%-4d,yes ', [12]);
Outputs are: This is a and yes

["." Prec] Specifies the precision, which works best 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 integer numbers, if the number of bits of prec such as Integer is small, then there is no effect on the other than the number of digits of the shape value, it will be in front of the integer value of 0 to complement the
Format (' This is%.7d ', [1234]);
Output: This is 0001234]

For a character type, just as opposed to an integer value, if the PREC is larger than the length of the string, it has no effect and, conversely, is smaller than the length of the string, which truncates the trailing character
Format (' This is%.2s ', [' 1234 ']);
The output is the this is 12, and the example above says:

Format (' This is%e ', [-2.22]);
The return is: This is-2.22000000000000e+000, how to remove the extra 0, this is OK

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

Well, the first one is finally finished, so you should be familiar with his application.

///////////////////////////////////////////////////////////////
The usage of two FormatDateTime
His statement is:

function FormatDateTime (const format:string; Datetime:tdatetime): string;
overload;

There is, of course, the same type as format, but this is only the first common one, and the format parameter is a formatted string. DateTime is the time type. The return value is a formatted string that focuses on the instruction character in the format parameter

C Displays the time in a short time format, which is all the representation of a number
FormatDateTime (' C ', now);
Output is: 2004-8-7 9:55:40

D corresponds to the date in the time, the date is one, and the two display two-bit
FormatDateTime (' d ', now);
The output may be 1~31

DD and d have the same meaning, but it is always displayed as a two-bit
FormatDateTime (' DD ', now);
The output may be 01~31

DDD shows the day of the week
FormatDateTime (' DDD ', now);
Output is: Saturday

DDDD and DDD show the same. But the above two may not be the same in other countries. DDDDD display date in short time format
FormatDateTime (' ddddd ', now);
Output is: 2004-8-7

DDDDDD Displays month and day in a long time format
FormatDateTime (' dddddd ', now);
The output is: August 7, 2004

E/EE/EEE/EEEE displays the year in the corresponding number of digits
FormatDateTime (' ee ', now);
Output: 04 (for 04)

M/mm/mmm/mmmm represents the Month
FormatDateTime (' m ', now);
Output is: 8
FormatDateTime (' mm ', now);
Output is 08
FormatDateTime (' mmm ', now);
Output is August
FormatDateTime (' MMMM ', now);
Output is August

Like DDD/DDDD, in other countries may be different yy/yyyy expressed year
FormatDateTime (' yy ', now);
Output is 04
FormatDateTime (' yyyy ', now);
The output is 2004,

H/hh,n/nn,s/ss,z/zzz hours, minutes, seconds, milliseconds, respectively

T display time in short time format
FormatDateTime (' t ', now);
Output is 10:17

TT displays time in a long time format
FormatDateTime (' TT ', now);
Output is 10:18:46

AMPM show morning or afternoon in a long time format
FormatDateTime (' ttampm ', now);
Output is: 10:22:57 a.m.

Presumably, if you want to add a normal string to format, you can use double quotation marks to separate the characters that are specifically defined, so that if you have a special character in a normal string, it will not be displayed as a time format:
FormatDateTime (' "Today is" C ', now);
Output: Today is 2004-8-7 10:26:58

Time can also be added "-" 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

/////////////////////////////////////////////////////////////////
Three. Usage of formatfloat

Common declarations:
function formatfloat (const format:string; value:extended): string; overload;

As above, the format parameter is the formatted instruction character, and value is the extended type, because it is the largest representation of all floating-point values, and if the parameters of the method are passed, such as double or other, then the save will not go out of scope.

The key is to look at the use of the format parameter
0 This specifies the corresponding number of digits of the instruction.
Like what:
Formatfloat (' 000.000 ', 22.22);
The output is 022.220.

Note that if the number of 0 in 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);
The output is: 22.22

However, if 0 of the fractional part is less than a multiple of the decimal value, the corresponding decimal and number of digits are truncated as follows:
Formatfloat (' 0.0 ', 22.22);
The output is: 22.2

You can also specify a comma in the integer 0, the integer number must be greater than 3, the comma will be a sentence
Formatfloat (' 0,000.0 ', 2222.22);
Output is: 2,222.2

If this
Formatfloat (' 000,0.0 ', 2222.22);
Its output is: 2,222.2

Pay attention to its regularity, #和0的用法一样, at present I have not measured what is different.

Formatfloat (' ##.## ', 22.22);
Output is: 22.00

E-scientific notation, look at a few examples and probably 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
You see, it's all on the right side of the E.

This method is not difficult, it is probably the case.

The above three methods are very common, there is no skill, just remember these specifications on the line.

Summarize the usage of format:

Format (' x=%d ', [n]);//' x=12 '//most common
Format (' x=%3d ', [n]);//' x=12 '//Specify width
Format (' x=%f ', [12.0]);//' x=12.00 '//floating-point number
Format (' x=%.3f ', [12.0]);//' x=12.000 '//Specify decimals
Format (' x=%8.2f ' [12.0])//' x=12.00 ';
Format (' x=%.*f ', [5,12.0]);//' x=12.00000 '//dynamic configuration
Format (' x=%.5d ', [+]);//' x=00012 '//Front add 0
Format (' x=%.5x ', [n]);//' x=0000c '//16 binary
Format (' X=%1:d%0:d ', [12,13]);//' x=1312 '//Use Index
Format (' x=%p ', [nil]);//' x=00000000 '//pointer
Format (' x=%1.1e ', [12.0]);//' x=1.2e+001 '//scientific notation
Format (' x=%% ', []);//' x=% '//Get "%"
S:=format ('%s%d ', [s,i]);//s:=s+strtoint (I);//Connection string

A detailed description of format and FormatDateTime functions in Delphi

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.