码迷,mamicode.com
首页 > 其他好文 > 详细

强制类型转换

时间:2016-04-05 00:47:54      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

语法:

(数据类型名)待转换的值;  强制转换的数据类型一定要相兼容!

例如:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!