format string formatting instructions for use in Delphi (GO)

Source: Internet
Author: User
Tags integer numbers truncated

Source: format string formatting instructions for use in Delphi (GO)

First, the use of the Format function

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 use, so here 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, which is a Variant array, which can have multiple parameters, and each parameter can be different.

As in the following example:

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

After returning is

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 an optional instruction character that is used to format type types.

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 square 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]);

Returned by: this is-2.22000000000000e+000

Wait a minute. If you reduce the precision of the number

G This can only correspond to floating-point type, and it will remove the extra number in the value

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 indicated to the two digits after the decimal, and so on, say how to eliminate this situation

Second, even if the decimal is not truncated, it will not be separated by commas like the integer part.

M coin type, but there is a better format method for currency types, here is simply formatting

In addition it corresponds only to floating-point values

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 first%d index is 0, the second%d is 1, so the character is displayed

Yes 12 13

And if you define this:

Format (' This is%1:d%0:d ', [12,13]);

Then the returned string becomes the

This is 13 12

Now do you understand that index in [index:] indicates that the parameter in args is displayed in the

Order

There is also the case if such 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, or 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, and here 2 is wrong.

[Width] Specifies the width of the value to be formatted, see an example to see

Format (' This is%4d ', [12]);

Output yes: This is 12

This is easier, 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 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]);

Lost 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 ']);

Output Yes 12

And the example above says:

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

Returned by: this is-2.22000000000000e+000

How to get rid of the extra 0, that's it.

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

The usage of two FormatDateTime

His statement is:

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

Of course, there is a kind of format, but here is only the first common

The format parameter is a formatted string. DateTime is the time type. The return value is a formatted string

Focus 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, may be different in other countries

YY/YYYY represents the year

FormatDateTime (' yy ', now);

Output is 04

FormatDateTime (' yyyy ', now);

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.

For example: 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

such as: formatfloat (' 0.0 ', 22.22);

The output is: 22.2

You can also specify a comma in the integer 0, which must be greater than 3, before a comma appears

Formatfloat (' 0,000.0 ', 2222.22);

Output is: 2,222.2

If such formatfloat (' 000,0.0 ', 2222.22);

Its output is: 2,222.2

Pay attention to its regularity

# As with 0 usage, I haven't measured the difference at the moment.

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.

format string formatting instructions for use in Delphi (GO)

Related Article

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.