C + + Converts a string type to int, float, and double type mainly in the following ways:

Source: Internet
Author: User

C + + Converts a string type to int, float, and double type mainly in the following ways:

# method One: Use StringStream

StringStream has been introduced in methods that convert int or float types to string types, and can also be used as a type of string to convert to a commonly used numeric type.

Demo:

[CPP]View Plaincopy
  1. #include <iostream>
  2. #include <sstream>//use StringStream to introduce this header file
  3. Using namespace std;
  4. Template functions: Converting a string variable to a common numeric type (this method is universally applicable)
  5. Template <class type>
  6. Type stringtonum (const string& str)
  7. {
  8. Istringstream ISS (str);
  9. Type num;
  10. ISS >> num;
  11. return num;
  12. }
  13. int main (int argc, char* argv[])
  14. {
  15. String str ("00801");
  16. cout << stringtonum<int> (str) << Endl;
  17. System ("pause");
  18. return 0;
  19. }

Output Result:

  801
Please press any key to continue ...

#方法二: Use the Atoi (), Atil (), atof () functions-----------------are actually conversions of char types to numeric types

Note : If string s is empty with Atoi, the return value is 0. It is not possible to determine if s is 0 or null

1. Atoi (): int atoi (const char * str);

Description: Parses the C string str interpreting its content as a integral number, which is returned as an int valu E.

parameter: str : C string beginning with the representation of a integral number.

return value: 1.  The successful conversion displays a value of type int.  2. the non-convertible string returns 0. 3. if the buffer overflows after the conversion, return Int_max orint_min

Demo:

[CPP]View Plaincopy
  1. #include <iostream>
  2. Using namespace std;
  3. int main ()
  4. {
  5. int i;
  6. char szinput [256];
  7. cout<<"Enter a number:" <<endl;
  8. Fgets (Szinput, stdin);
  9. i = atoi (szinput);
  10. cout<<"The value entered is:" <<szInput<<endl;
  11. cout<<"The number convert is:" <<i<<endl;
  12. return 0;
  13. }

Output:

Enter a number:48

The value entered is:48

The number convert is:48

2.aotl (): Long int atol (const char * str);

Description: C string str interpreting its content as an integral number, which is returned as a long int value (usage and at Oi function similar, return value is long int)

3.atof (): Double atof (const char * str);

Parameters: C string beginning with the representation of a floating-point number.

return Value:  1. Conversion successfully returns a value of type Doublel 2. Cannot convert, return 0.0. 3. Cross-border, return to huge_val

Demo:

[CPP]View Plaincopy
  1. /* Atof example:sine Calculator */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. int main ()
  6. {
  7. double n,m;
  8. double pi=3.1415926535;
  9. char szinput [256];
  10. printf ( "Enter degrees:");
  11. Gets (Szinput);
  12. convert//char type to double type
  13. n = atof (szinput);
  14. m = sin (n*pi/180);
  15. printf ( "The sine of%f degrees is%f\n", N, m);
  16. return 0;
  17. }

C + + Converts a string type to int, float, and double type mainly in the following ways:

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.