[C ++] cstring. Format usage in MFC

Source: Internet
Author: User
Tags truncated

In the MFCProgramUsing cstring to process strings is a good choice. Cstring can process both Unicode-standard strings and ANSI-standard strings. The format method of cstring makes it very convenient for us to convert strings. For example, to convert common numbers such as int, float, and double to a cstring, only one line is required.CodeYou can achieve this.

First, let's take a look at the format characters used for conversion:

% C single character

% D decimal INTEGER (INT)

% LD decimal INTEGER (long)

% F decimal floating point number (float)

% Lf decimal floating point number (double)

% O octal values

% S string

% U unsigned decimal number

% X hexadecimal number

1. Convert int to cstring:

Cstring STR;

Int number = 15;

// STR = "15"

Str. Format (_ T ("% d"), number );

// STR = "15" (there are two spaces in front; 4 indicates that 4 digits will be occupied. If the number exceeds 4 digits, all numbers will be output and will not be truncated)

Str. Format (_ T ("% 4D"), number );

// STR = "0015" (. 4 indicates that 4 digits will be occupied. If the number exceeds 4 digits, all numbers will be output and will not be truncated)

Str. Format (_ T ("%. 4D"), number );

The method for converting long to cstring is similar to above. You only need to change % d to % lD.

2. Convert double to cstring:

Cstring STR;

Double num = 1.46;

// STR = "1.46"

Str. Format (_ T ("% lf"), num );

// STR = "1.5" (. 1 indicates that one digit is left after the decimal point, and more than one digit is rounded off)

Str. Format (_ T ("%. 1lf"), num );

// STR = "1.4600"

Str. Format (_ T ("%. 4f"), num );

// STR = "1.4600" (there is a space in front)

Str. Format (_ T ("% 7.4f"), num );

The float conversion method to cstring is similar to the preceding method. You can change lf % to F %.

3. Convert the decimal number to octal:

Cstring STR;

Int num = 255;

// STR = "377"

Str. Format (_ T ("% O"), num );

// STR = "00000377"

Str. Format (_ T ("%. 8o"), num );

Format It is a very common but annoying method. The following is a complete overview of it for your query:

Format a string Forma ("% d", 12) It refers to a character formatted by an integer. ( I think it is to keep its shape unchanged. )
1 ). The format is always % Character , The formats of different data types are as follows: % Description after no. :
D Output signed decimal number  
O Output unsigned octal number  
X Output unsigned hexadecimal number  
U Number of output unsigned characters  
C Output Single Character  
S Output a string of characters  
F Output real number ( 6 Decimal places)  
E Output real numbers in exponential form  
G Select F And E The output format is smaller than the output width. 0
LD Input and Output Long Type data  
Lf Input and Output Double Type data  
M The data output width is M
. N Number of output decimal places N


 

 

 

 

 

 

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.