一、类型转换(相兼容)
隐式类型转换:
我们要求等号两遍参与运算的操作数的类型必须一致,如果不一致,满足下列条件会发生自动类型转换,或者称之为隐式类型转换。
两种类型兼容
例如:int 和 double 兼容(都是数字类型)
目标类型大于源类型
例如:double > int 小的转大的(int转double)
显示类型转换:
1、两种类型相兼容 int--double
2、大的转成小的 double----int
语法:
(待转换的类型)要转换的值;
总结:
自动类型转换:int---->double
显示类型转换:double--->int
***对于操作数而言,如果一个操作数为double类型,则整个表达式可以提升为double类型
二、Convert类型转换(不兼容的两个类型 )
类型如果相兼容的两个变量,可以使用自动类型转换或者强制类型转换,但是,如果两个类型的变量不兼容,比如 string与int或者string 与double,这个时候我们可以使用一个叫做Convert的转换工厂进行转换(内部实质上也是调用的Parse()方法)。注意:使用Convert进行类型转换,也需要满足一个条件:
面儿上必须要过的去。string-->int/double string str ="1234";(可以转) string str= "abcd"(不可以转报异常)
转换结果:使用convert进行转换,成功了就转换成了,失败了就抛异常
三、parse()方法的类型转换(不兼容的两种类型)
常用的方法:
public static int Parse(string s);
public static bool TryParse(string s, out int result);
public static double Parse(string s);
public static bool TryParse(string s, out double result);
结果:使用parse();方法进行类型的转换成功了就转换成了,失败了就抛异常
使用TryParse();方法进行类型的转换成功了就转换成了,失败了返回一个bool类型的false值,且还会给out参数赋一个要转换成的类型的默认值(int 默认0,double默认0)
1、显隐式类型转换:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 类型转换
{
class Program
{
static void Main(string[] args)
{
int t_short = 30;
int pants = 80;
int totalMoney = 4 * t_short + 2 * pants;//int类型的
Console.WriteLine("总价钱: "+totalMoney);
//对于操作数而言,如果一个操作数为double类型,则整个表达式可以提升为double类型
double realMoney = totalMoney * 0.88;//等号两边参与运算的数据类型不一样此处发生隐式类型转换
Console.WriteLine("打0.88折后的价钱:"+ realMoney);
Console.WriteLine("======分割线=======");
int a = 20;
double b = a;
Console.WriteLine("自动(隐式)类型转换: "+b);
Console.WriteLine("======分割线======");
//(待转换的类型)要转换的值;
double b2 = 3.14;
int a2 = (int)b2;
Console.WriteLine("强制(显示)类型转换: "+ a2);
Console.ReadKey();
}
}
}
运行结果:
***对于操作数而言,如果一个操作数为double类型,则整个表达式可以提升为double类型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 类型转换1
{
class Program
{
static void Main(string[] args)
{
int n1 = 10;
int n2 = 3;
double m1 = n1 / n2;//还是int
//对于操作数而言,如果一个操作数为double类型,则整个表达式可以提升为double类型
double m2 = n1 * 1.0 / n2;//1.0将整个表达式(n1 * 1.0 / n2)提升为double类型
Console.WriteLine("没有提升为double类型的结果: "+m1);
Console.WriteLine("提升为double类型的结果:"+m2);
double n3 = 5;
int n4 = 2;
double m3 = n3 / n4;//n3提升为double类型,整个表达式(n3/n4)也提升为double类型
Console.WriteLine("提升为double类型后的结果:"+ m3);
Console.ReadKey();
}
}
}
运行结果:
2、convert类型转换:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 不兼容convert类型转换
{
class Program
{
static void Main(string[] args)
{
//显式类型转换,隐式类型转化
//int-->double double--int
string str = "123";
int iNum = Convert.ToInt32(str);
Console.WriteLine("字符串\"123\"转int: "+iNum);
Console.WriteLine("======分割线======");
double dNum = Convert.ToDouble(str);
Console.WriteLine("字符串\"123\"转double:"+dNum);
Console.ReadKey();
}
}
}
运行结果:
convert练习:输入学生语数外,输出总成绩平均成绩
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace convert类型转换练习
{
class Program
{
static void Main(string[] args)
{
//用户输入语文,数学,英语三门成绩2、输出总成绩,平均成绩
Console.WriteLine("请输入学生姓名");
string name = Console.ReadLine();
Console.WriteLine("请输入学生的语文成绩");
string strChineseScore = Console.ReadLine();
Console.WriteLine("请输入学生的数学成绩");
string strMathScore = Console.ReadLine();
Console.WriteLine("请输入学生的英语成绩");
string strEnglishScore = Console.ReadLine();
//90 92 94
//字符串相加的话会变成连接 需要转型
int chineseScore = Convert.ToInt32(strChineseScore);
int mathScore = Convert.ToInt32(strMathScore);
int englishScore = Convert.ToInt32(strEnglishScore);
Console.WriteLine("姓名:{0} 总成绩是: {1} 平均成绩是: {2}", name, (chineseScore + mathScore + englishScore), (chineseScore + mathScore + englishScore)/3);
Console.ReadKey();
}
}
}
运行结果:
convert练习:输入学生语数外,输出总成绩 平均成绩 平均成绩保留两位小数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace convert练习1
{
class Program
{
static void Main(string[] args)
{
//用户输入语文,数学,英语三门成绩2、输出总成绩,平均成绩保留两位小数
Console.WriteLine("请输入学生姓名");
string name = Console.ReadLine();
Console.WriteLine("请输入学生的语文成绩");
string strChineseScore = Console.ReadLine();
Console.WriteLine("请输入学生的数学成绩");
string strMathScore = Console.ReadLine();
Console.WriteLine("请输入学生的英语成绩");
string strEnglishScore = Console.ReadLine();
int chineseScore = Convert.ToInt32(strChineseScore);
int mathScore = Convert.ToInt32(strMathScore);
int englishScore = Convert.ToInt32(strEnglishScore);
//1.0将将整个表达式提升为double类型
double avgScore = (chineseScore + englishScore + mathScore) * 1.0 / 3;
Console.WriteLine("姓名:{0} 总成绩是: {1} 平均成绩是: {2} 平均成绩保留两位小数: {3:0.00}", name, (chineseScore + mathScore + englishScore), avgScore, avgScore);
Console.ReadKey();
}
}
}
运行结果:
parse()方法的类型转换
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace parse类型转换
{
class Program
{
static void Main(string[] args)
{
string str = "123";
int iNum = int.Parse(str);
double dNum = double.Parse(str);
Console.WriteLine("字符串\"123\"转换成int类型的值是: "+iNum);
Console.WriteLine("======分割线======");
Console.WriteLine("字符串\"123\"转换成double类型的值是: " + dNum);
Console.ReadKey();
}
}
}
运行结果:
使用TryParse()方法进行类型转换:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TryParse类型转换
{
class Program
{
static void Main(string[] args)
{
string str = "123abc";
//此处可以不复初始值。它将作为out多余返回参数的实参,此参数会在传进的方法中赋默认初始值(int 0/double 0)
int iNum =100;
bool b = int.TryParse(str, out iNum);
Console.WriteLine("转换是否成功: {0}\nout多余返回的参数值(即由字符串\"123abc\"转换成int类型的值): {1}",b, iNum);
Console.ReadKey();
}
}
}
运行结果: