What is type conversion?
Conversion between different data types, such as: converting int type to string type
Why do you need type conversion?
The user input received from the console is of string type. If you want to calculate, you need to convert the received content to a numeric type.
What is an implicit conversion? As shown below
What is a forced conversion? As shown below
1. Code display
1. Implicit conversion : int——>double
int num1 = 10; //int类型变量
int num2 = 3; //int类型变量
double sum = num1*0.1 / num2; //double类型变量
Console.WriteLine("{0:0.00}",sum); //使用占位符{0:0.00}表示结果保留两位小数,并在控制台输出
Console.ReadKey();
Output result: 0.33
2. Force conversion
①, double——>int: add (int) before the variable
double number = 10.6; //double类型变量并赋值
int num = (int)number; //将double类型变量转换为int类型,变量前面加(int)
Console.WriteLine(num); //在控制台输出结果
Console.ReadKey();
Output result: 10
②, int——>string: variable name to be converted.to type to be converted
int num = 10; //int类型变量
string result = num.ToString(); //转换为string类型变量,转换的变量名.to要转换的类型
Console.WriteLine(result);
Console.ReadKey();
Output result: 10
③、string——>int:
Method 1: Convert conversion factory
method |
Description |
Convert.ToInt16() |
Convert to integer (short) |
Convert.ToInt32() |
Convert to integer (int) |
Convert.ToInt64() |
Convert to integer (long) |
Convert.ToChar () |
Convert to character type (char) |
Convert.ToString() |
Convert to string type (string) |
Convert.ToDateTime() |
Convert to date (datetime) |
Convert.ToDouble() |
Convert to double precision floating point (double) |
Conert.ToSingle() |
Convert to single precision floating point (float) |
Note: To whomever the user wants to convert
Console.WriteLine("请输入你的语文成绩"); //系统提示用户输入
String strChinese = Console.ReadLine(); //接收用户输入的内容
int chinese = Convert.ToInt32(strChinese); //接收到的内容位字符串型数据,转换为int类型
Console.WriteLine("您的语文成绩为:" + chinese );//在控制台输出
Console.ReadKey();
Output result: your language score is: 90
Method two, int.Parse (string variable to be converted)
try //try catch语句,尝试下面的语句
{
Console.WriteLine("请输入一个数字"); //提示用户输入内容
string strAge = Console.ReadLine(); //接收用户输入内容
int result = int.Parse(strAge); //将接收的内容转换为int类型数值
Console.WriteLine(result * 2); //输出结果
}
catch //如果出现异常,走下面的语句
{
Console.WriteLine("您输入的内容无法转换成数字"); //输出错误提示语
}
Console.ReadKey();
Output (exceptions): Enter a number
giggle giggle
you entered is not correct
Method three, int.TryParse (string variable to be converted)
Console.WriteLine("请输入一个数字"); //提示用户输入内容
string strAge = Console.ReadLine(); //接收用户输入的内容
int number = 0;
bool result = int.TryParse(strAge, out number); //定义一个布尔类型变量,进行异常处理
if (result) //如果为trye则走句代码
{
Console.WriteLine(number * 2); //输出结果
}
else //否则
{
Console.WriteLine("您输入的内容不正确");
}
Console.ReadKey();
Output (exceptions): Enter a number
giggle giggle
you entered is not correct
2. Summary:
int—>double |
Variable*0.1 |
double sum= number1 * 0.1 / number2; |
double—>int |
(int). Variable name |
int number = (int) num; |
int—>string |
Variable name.toString() |
string number = num.toString(); |
string—>int |
Convert.to conversion type (variable to be converted) |
int number = Convert.toInt32(num); |
double—>float |
(float) decimal |
float number = (float)0.134; |
Three, expansion:
The difference between Convert, Parse, TryParse in C#(int):