C + + Doulbe/float/int conversion to CString method __c++

Source: Internet
Author: User
Tags truncated
In MFC programs, you can use the Format method to easily convert numeric types such as int, float, and double to CString strings. The following is a format description supported by the CString format:%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 number
%s string
%u unsigned decimal digits
%x Hex Number 1, int conversion to CString

CString str;
int number=15;

Str= "15"
Str. Format (_t ("%d"), number);

Str= "15" (preceded by two spaces; 4 indicates that 4 digits will be occupied, and if the number exceeds 4, 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, and if the number exceeds 4 digits, all numbers will be output and will not be truncated)
Str. Format (_t ("%.4d"), number);

A long conversion to CString is similar to the above, with the need to change%d to%ld. 2, double conversion to CString

CString str;
Double num=1.46;

Str= "1.46"
Str. Format (_t ("%LF"), num);

Str= "1.5" (. 1 means to leave 1 digits after the decimal point, rounded by more than 1 digits after the decimal point)
Str. Format (_t ("%.1LF"), num);

Str= "1.4600"
Str. Format (_t ("%.4f"), num);

Str= "1.4600" (preceded by 1 spaces)
Str. Format (_t ("%7.4f"), num);

Float conversion to CString method is similar to the above, the lf% to f% on it. 3. Convert decimal number to octal system

CString str;
int num=255;

Str= "377"
Str. Format (_t ("%o"), num);

Str= "00000377"
Str. Format (_t ("%.8o"), num);

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.