Type conversion in C #

Source: Internet
Author: User

1. simple numeric type conversion

In Java, we know there are two conversion forms: automatic type conversion and forced type conversion. in C #, we can call these two types implicit type conversion.

Conversion and explicit type conversion. The conversion method is the same as that in Java.

1. implicit type conversion

For the value type, as long as the value range of type A is completely included in type B, type A can be implicitly converted to type B.

For example, an integer (INT) A can be implicitly converted to a floating point (double ).

2. Explicit type conversion

In contrast to implicit conversion, an explicit type conversion is required if the value range is large and the value range is small.

Double score = 85.5;

Int add = 2;

Int finalscore = (INT) score + Add;

The above two conversion methods are mainly applicable to the conversion between numerical types.

2. Conversion between value types and strings

The preceding method is applicable to the conversion between numeric types.

How can we convert the value type to the string type?

1. Convert string to numeric type

In the C # Basic Course, we know the following method to accept the value type from the console:

Int score = int. parse (console. Readline ());

You can use the parse () method to convert a string to a numeric value. Different numeric types have their own parse () methods.

String to integer

Int. parse (string );

Convert string to single-precision floating point

Float. parse (string );

Convert string to double-precision floating point

Double. parse (string );

Note: The string to be converted must be a valid expression of a number.

2. Convert numeric type to string type

To convert a value to a string, use the tostring () method.

Int age = 20;

String myage = age. tostring ();

Iii. Use the convert class for type conversion

The aboveThe parse () method is used to convert the string type to the numeric type., Let's get to know a more powerful convert class, which can interact with each other between basic types.Conversion. The convert class provides a static method for each type.

Convert. toint32 (); // convert to an integer

Convert. tosingle (); // convert to single-precision floating point

Convert. todouble (); // convert to double-precision floating point

Convert. tostring (); // convert to string type

The convert class provides a static method for each type of conversion, so it can be called directly by the type. method name.

For example:

Code:
  1. Class demo
  2. {
  3. Static VoidMain (String[] ARGs)
  4. {
  5. DoubleScore = 85.6;
  6. IntMyint;
  7. FloatMyfloat;
  8. StringMystring;
  9. Console. writeline ("The original type is double: {0 }", Score );
  10. Myint = convert. toint32 (score );
  11. Myfloat = convert. tosingle (score );
  12. Mystring = convert. tostring (score );
  13. Console. writeline ("After conversion :");
  14. Console. writeline ("Int/T float/T string");
  15. Console. writeline ("{0}/t {1}/t {2 }", Myint, myfloat, mystring );
  16. }
  17. }

Running result:

Int float string

86 85.6 85.6

It is worth noting that when we explicitly convert the double type to the int type, the result is 85, but when we use the convert class for conversion, the result is 86.The conclusion is that when explicit type conversion is used, decimal places will be ignored, which is equivalent to using the function floor () in SQL. The convert class is used to convertThe rounding method is used.

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.