[黑马程序员]类型转换总结

13 篇文章 0 订阅

 

------------------------------ASP.Net+Android+IO开发 .Net培训 期待与您交流!------------------------------

 

学习过C#与java的基础视频教程后,对于类型转换总结如下:

一.隐式类型转换

(一)概念:无需声明就能进行的转换。

                int i = 123;

                decimal money = i; //数字转货币

                float f = i; //整型转单精度

(二)隐式类型转换表:

源类型

目标类型                                                                           

sbyte           

short 、intlongfloatdouble 或 decimal

byte

short 、ushortintuintlongulongfloatdouble 或decimal

short

int 、longfloatdouble 或 decimal

ushort

int 、uintlongulongfloatdouble 或 decimal

int

long 、floatdouble 或 decimal

uint

long 、ulongfloatdouble 或 decimal

long

float 、double 或 decimal

char

ushort 、intuintlongulongfloatdouble 或 decimal

float

double

ulong

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)一样

 

二.显式类型转换

(一)概念:及强制类型转换,需要在代码中明确的申明要转换的类型。

(二)显式类型转换表:

源类型

目标类型

sbyte         

byte 、ushortuintulong 或 char

byte

Sbyte 或 char

short

sbyte 、byteushortuintulong 或 char

ushort

sbyte 、byteshort 或 char

int

sbyte 、byteshortushortuintulong 或 char

uint

sbyte 、byteshortushortint 或 char

long

sbyte 、byteshortushortintuintulong 或 char

ulong

sbyte 、byteshortushortintuintlong 或 char

char

sbyte 、byte 或 short

float

sbyte 、byteshortushortintuintlongulongchar或 decimal

double

sbytebyteshortushortintuintlongulongcharfloat或 decimal

decimal

sbytebyteshortushortintuintlongulongcharfloat或 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培训 期待与您交流!------------------------------

     

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值