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:
-
- Class demo
-
- {
- Static VoidMain (String[] ARGs)
-
- {
-
- DoubleScore = 85.6;
- IntMyint;
-
- FloatMyfloat;
-
- StringMystring;
-
- Console. writeline ("The original type is double: {0 }", Score );
-
-
- Myint = convert. toint32 (score );
-
- Myfloat = convert. tosingle (score );
- Mystring = convert. tostring (score );
-
-
- Console. writeline ("After conversion :");
-
- Console. writeline ("Int/T float/T string");
-
-
- Console. writeline ("{0}/t {1}/t {2 }", Myint, myfloat, mystring );
-
- }
- }
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.