C # programming type conversion

Source: Internet
Author: User

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 ".

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.