标签:
语法:
(数据类型名)待转换的值; 强制转换的数据类型一定要相兼容!
例如:
1,
double pi=3.14;
int number=(int)pi;
Console.WriteLine("pi={0}",number);
Console.ReadKey();
2,
double coat = 35, pants = 120;
double total = coat * 3 + pants * 2;
int riduction = (int)(total * 0.88); //此处一定要加括号先进行相*后在转换
Console.WriteLine("购物总价为:{0}\n打折后的价格为:{1}", total, riduction);
Console.ReadKey();
另外下面2种情况是错误的不能强制转换(强制转换的数据类型一定要相兼容!)
例1;
int number=(int)"22"; //字符串不能强制转化成int类型
int input=(int)Console.ReadLine();//获取屏幕上输入内容是字符串
或者也这样也是错误的 string input=(int)Cosole.ReadLine();
标签:
原文地址:http://www.cnblogs.com/swlq/p/5353480.html