------------------------------ASP.Net+Android+IO开发 .Net培训 期待与您交流!------------------------------
学习过C#与java的基础视频教程后,对于类型转换总结如下:
一.隐式类型转换
(一)概念:无需声明就能进行的转换。
int i = 123;
decimal money = i; //数字转货币
float f = i; //整型转单精度
(二)隐式类型转换表:
源类型 | 目标类型 |
---|---|
short 、int、long、float、double 或 decimal | |
short 、ushort、int、uint、long、ulong、float、double 或decimal | |
int 、long、float、double 或 decimal | |
int 、uint、long、ulong、float、double 或 decimal | |
long 、float、double 或 decimal | |
long 、ulong、float、double 或 decimal | |
float 、double 或 decimal | |
ushort 、int、uint、long、ulong、float、double 或 decimal | |
double | |
float 、double 或 decimal |
(三)备注:
(1)从int, uint, long,或ulong到float,以及long或ulong到double的转换可能导致精度损失,但不会影响它的数量级。其他的隐式转换不会丢失任何信息。
(2)不存在到 char 类型的隐式转换。
(3)不存在浮点型与 decimal 类型之间的隐式转换。
(4)int 类型的常数表达式可转换为 sbyte、byte、short、ushort、uint 或 ulong,前提是常数表达式的值处于目标类型的范围之内。
(四)视频教程重点说明:
(1)decimal d = 3.14; 是错的
decimal d = 3.14m;
double db = d; 是错的
分析: double 正负5.0乘以10的-324次方 到 正负1.7乘以10的308次方
精度为15到16位
decimal 正负1.0乘以10的-28次方 到 正负7.9乘以10的28次方
精度28到29位有效位
decimal d = 3.14; 3.14是double 它的取值范围要比decimal大的多 所以是错的
decimal d = 3.14m;
double db = d; 3.14m是decimal类型,它的精度要比double准确的多,所以是错的
(2)int a=10,b=3;
double d = a/b;
d 的结果是3 原因: int / int 之后 还是int 然后要赋给double的d 所以3会提升为3.0 所以打印出的结果是3
如果想要打印出来的结果为3.3333....呢?
可以这样做
int a=10,b=3;
double d = 1.0*a/b;即可 结果就是3.33333333333333
(3)'a' + 1 结果为98
(4)byte b=3;
b = b+2; 是错的
分析: b 是一个8位, 2是4个8位,b+2运算的时候 b将自动提升为4个八位 但最终要再赋值给b(1个八位)所以就挂了
(5)b = (byte)b +2; 也是错的 原因和(4)一样
二.显式类型转换
(一)概念:及强制类型转换,需要在代码中明确的申明要转换的类型。
(二)显式类型转换表:
源类型 | 目标类型 |
---|---|
byte 、ushort、uint、ulong 或 char | |
Sbyte 或 char | |
sbyte 、byte、ushort、uint、ulong 或 char | |
sbyte 、byte、short 或 char | |
sbyte 、byte、short、ushort、uint、ulong 或 char | |
sbyte 、byte、short、ushort、int 或 char | |
sbyte 、byte、short、ushort、int、uint、ulong 或 char | |
sbyte 、byte、short、ushort、int、uint、long 或 char | |
sbyte 、byte 或 short | |
sbyte 、byte、short、ushort、int、uint、long、ulong、char或 decimal | |
sbyte、byte、short、ushort、int、uint、long、ulong、char、float或 decimal | |
sbyte、byte、short、ushort、int、uint、long、ulong、char、float或 double |
(三)备注:
(1)显式数值转换可能导致精度损失或引发异常。
(2)将 decimal 值转换为整型时,该值将舍入为与零最接近的整数值。 如果结果整数值超出目标类型的范围,则会引发 OverflowException。
(3)将 double 或 float 值转换为整型时,值会被截断。 如果该结果整数值超出了目标值的范围,其结果将取决于溢出检查上下文。 在 checked 上下文中,将引发 OverflowException;而在 unchecked 上下文中,结果将是一个未指定的目标类型的值。
(4)将 double 转换为 float 时,double 值将舍入为最接近的 float 值。 如果 double 值因过小或过大而使目标类型无法容纳它,则结果将为零或无穷大。
(5)将 float 或 double 转换为 decimal 时,源值将转换为 decimal 表示形式,并舍入为第 28 个小数位之后最接近的数(如果需要)。 根据源值的不同,可能产生以下结果:
(a)如果源值因过小而无法表示为 decimal,那么结果将为零。
(b)如果源值为 NaN(非数字值)、无穷大或因过大而无法表示为 decimal,则会引发 OverflowException。
(c)将 decimal 转换为 float 或 double 时,decimal 值将舍入为最接近的 double 或 float 值。
(四)视频教程重点说明:
(1)float f = 123.45;
int i = (int)f;
或
float f = 123.45;
int i = Convert.ToInt32(f);
其中float f = 123.45; 这句报错
原因 123.45 为double类型 所以必须写成float f = (float) 123.98;
而不管是123.45 还是 123.89 最终打印出的结果都为 123
(2)(char)5 打印出的结果是梅花
(3)(char)('a' + 1) 这个打印出的结果是 b
(4)(int)"3333" 这句报错 因为int 和 字符串不想兼容
强制转换不行,但可以用另一种方法
Convert.ToInt32("33333"); 即可
但Convert.ToInt32("jdkj"); 则会报错
此文章今后还会继续总结......
------------------------------ASP.Net+Android+IO开发 .Net培训 期待与您交流!------------------------------