The format formatter function of Delphi

Source: Internet
Author: User
Tags integer numbers

Reprinted from: http://www.cnblogs.com/mumble/archive/2011/05/25/2056462.html

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:

  In fact, when you look at the Format function of Delphi, you can compare the formatting of the printf function in C with the knowledge associated with it.

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 ']);//Return 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 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 minus the absolute number, such as:

Format (' This is%u ', [-2]);//return: 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]);//return: 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 of values, such as

Format (' This is%g ', [02.200]);//return: 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;beginx:=99;p:[email protected]; Edit1.text:=format (' This is%p ', [P]); End;//edit1 content is: the 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 ', [12,13]);//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, or it will cause an exception (because it can cause an exception, so you should use try ... except ... method to catch a possible exception and handle it) 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]);//outputs are:   This is 12, which 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 Yes: This is 12

["-"] This specified parameter is left-aligned, and [width] together to see the effect:

Format (' This is%-4d,yes ', [12]);//Output Yes: This is a   , yes

["." Prec] Specifies the precision, which works best for floating point numbers:

Format (' This is%.2f ', [' 1.1234]);//Output This is 1.12Format (' 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 Yes: 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 this is 12, and the example above says:

 

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

  

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

Well, the first one is finally finished, it should be familiar with its application.

The format formatter function of 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.