C + + format usage [go]

Source: Internet
Author: User
Tags integer numbers truncated

Format is a very common, but it seems very annoying method, the following is its complete overview, for everyone to query:

One, the string

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");
After returning is
My name is wind

Now look at the details of the format parameter:
Normal strings can be written in format, such as "My Name is"
However, some format instruction characters have special meanings, 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:
DA decimal number that represents an integer value
uAndDis an integer value, but it is unsigned, and if its corresponding value is negative, it is returned
is a 2 of the 32 times minus this absolute number
Such as:Format ("This is%u",-2);
The returned is:This is 4294967294
F
Corresponding floating-point number
eScientific notation, corresponding to integers and floating-point numbers,
Like whatFormat ("This is%e",-2.22);
The returned is:This is-2.220000e+000
Wait a minute. If you reduce the precision of the number
gThis only corresponds to a floating-point type, and it removes the extra number from the value
Like whatFormat ("This is%g", 02.200);
The returned is:This is 2.2
N
can only correspond to floating point type, the value is converted into the form of numbers. 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.
mCoin 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, the returned value is the address of the pointer, expressed in 16-binary form
For example:
Format ("This is%p", p);
The contents of Edit1 are:This is 0012f548
sCorresponds to a string type, no more talking.
xMust be an integer value, returned in 16-binary form
Format ("This is%x", 15);
Return is:This is F

Type is complete, the following describes the format of theTypeThe instructions:
[index: "]How do you say 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
That's it.This is 12 13

And if you define this:
Format ("This is%1:d%0:d", 12,13);
Then the returned string becomes the
This is
Now do you understand that index in [index:] indicates that the parameter in args is displayed in the
Order

There is another case, if thisFormat ("%d%d%0:d%d", 1, 2, 3, 4);
will return1 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 asFormat ("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 M", 12);
The output is: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", 12);
The output is:This is 12
["-"]This specifies that the parameter is left-aligned, and the [width] is the most visible effect:
Format ("This is%-4d,yes", 12);
The output is: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.12
Format (' This is%.7f ', [' 1.1234]);
Lost This is 1.1234000


For integer numbers, if the number of bits of the prec, such as the Integer, is small, there is no effect
Conversely, larger than the number of digits of the shaping value, the value of the integer will be preceded by 0
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 type
is not effective, and vice versa is less than the length of the string type, it 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]);

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

///////////////////////////////////////////////////////////////
two usage of 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.

So, if you want to add a normal string to format, you can use double quotes to separate those
A specific character, so that a normal string, if it contains a special character, will not be displayed as
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" YYMMDD ', 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;
Same as above. The format parameter is the formatted instruction character, value is the extended type
Why is this type, because it is the largest representation of all floating-point values, if the parameters of the method are passed in
For example, double or other, you can save without exceeding the range.

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, 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 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.

C + + format usage [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.