1. Any programming language has related data types. C # is no exception. Its basic data types include int, short, long, float, double, and string. Data types can be converted to each other. However, during the conversion process, note that small data types can be converted to large data types, but large data types cannot be converted to small data types. For example, int type can be converted to float type, but float type may not be converted to int type. At least this is true for C and C, but it has obviously changed in C, it seems that Microsoft has allowed such a format.
For example:
Double dbl_num = 12345678910.456;
Int k = (int) dbl_num; // forced conversion is used here
If the above Code is forcibly converted to the int type in C and C, an error will certainly occur, but now there will be no errors in C #, but the converted value is usually an overflow value, is not proficient. Note This.
2. Use another type conversion method, such as int. parse () and int32.parse.
For example, string str = "100 ″;
Int I = int. Parse (str );
Note: The type of str to remove quotation marks must be the same as that of *. Parse. If you change 100 to 100.78, that is, to the float type, an error will be reported during the runtime, "the input string format is incorrect ."
3. suffix conversion, such as k. toString (), is generally used for other types such as string or date.
Int I = 100;
String s = I. ToString ();
4. Convert using the Convert class. This class basically supports conversion between different types.
String str = "100 ″;
Int I = Convert. ToInt16 (str );
Note: The type of str to remove quotation marks must be the same as that of Convert. If you change 100 to 100.78, that is, to the float type, an error will be reported during the runtime, "the input string format is incorrect ".