Conversion of int, float, double, and string in C ++ 11, floatdouble

Source: Internet
Author: User

Conversion of int, float, double, and string in C ++ 11, floatdouble

In C ++ 11, you can use the std: to_string () function to convert the value to the string format, which is very convenient.

Select cplusplus.com from the following parts.

Std: to_string
string to_string (int val);string to_string (long val);string to_string (long long val);string to_string (unsigned val);string to_string (unsigned long val);string to_string (unsigned long long val);string to_string (float val);string to_string (double val);string to_string (long double val);
Convert numerical value to string

Returns a string with the representationVal.

Example
 1 // to_string example 2 #include <iostream>   // std::cout 3 #include <string>     // std::string, std::to_string 4  5 int main () 6 { 7   std::string pi = "pi is " + std::to_string(3.1415926); 8   std::string perfect = std::to_string(1+2+4+7+14) + " is a perfect number"; 9   std::cout << pi << '\n';10   std::cout << perfect << '\n';11   return 0;12 }
Output

Pi is 3.141593

28 is a perfect numbe

References

[1]Http://www.cplusplus.com/reference/string/to_string/

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.