A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 杨靖 中级黑马   /  2013-10-21 16:11  /  1779 人查看  /  2 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 杨靖 于 2013-10-21 16:13 编辑

显式数值转换表(C# 参考)

目标

sbyte

byte 、ushort、uint、ulong 或 char

byte

Sbyte 或 char

short

sbyte 、byte、ushort、uint、ulong 或 char

ushort

sbyte 、byte、short 或 char

int

sbyte 、byte、short、ushort、uint、ulong 或 char

uint

sbyte 、byte、short、ushort、int 或 char

long

sbyte 、byte、short、ushort、int、uint、ulong 或 char

ulong

sbyte 、byte、short、ushort、int、uint、long 或 char

char

sbyte 、byte 或 short

float

sbyte 、byte、short、ushort、int、uint、long、ulong、char 或 decimal

double

sbyte 、byte、short、ushort、int、uint、long、ulong、char、float 或 decimal

decimal

sbyte 、byte、short、ushort、int、uint、long、ulong、char、float 或 double


备注显式数值转换可能导致精度损失或引发异常。
将 decimal 值转换为整型时,该值将舍入为与零最接近的整数值。 如果结果整数值超出目标类型的范围,则会引发 OverflowException
将 double 或 float 值转换为整型时,值会被截断。 如果该结果整数值超出了目标值的范围,其结果将取决于溢出检查上下文。 在 checked 上下文中,将引发OverflowException;而在 unchecked 上下文中,结果将是一个未指定的目标类型的值。
将 double 转换为 float 时,double 值将舍入为最接近的 float 值。 如果 double 值因过小或过大而使目标类型无法容纳它,则结果将为零或无穷大。
将 float 或 double 转换为 decimal 时,源值将转换为 decimal 表示形式,并舍入为第 28 个小数位之后最接近的数(如果需要)。 根据源值的不同,可能产生以下结果:如果源值因过小而无法表示为 decimal,那么结果将为零。
如果源值为 NaN(非数字值)、无穷大或因过大而无法表示为 decimal,则会引发 OverflowException。
将 decimal 转换为 float 或 double 时,decimal 值将舍入为最接近的 double 或 float 值。
隐式数值转换表(C# 参考)
  • 目标
    sbyteshort 、int、long、float、double 或 decimal
    byteshort 、ushort、int、uint、long、ulong、float、double 或 decimal
    shortint 、long、float、double 或 decimal
    ushortint 、uint、long、ulong、float、double 或 decimal
    intlong 、float、double 或 decimal
    uintlong 、ulong、float、double 或 decimal
    longfloat 、double 或 decimal
    charushort 、int、uint、long、ulong、float、double 或 decimal
    floatdouble
    ulongfloat 、double 或 decimal

    备注
  • 可能从转换中丢失精度,但不是模int, uint, long,或ulong到float和long或ulong到double。
  • 不存在到 char 类型的隐式转换。
  • 不存在浮点型与 decimal 类型之间的隐式转换。
  • int 类型的常数表达式可转换为 sbyte、byte、short、ushort、uint 或 ulong,前提是常数表达式的值处于目标类型的范围之内。转载自MSDN








评分

参与人数 1技术分 +2 收起 理由
追溯客 + 2

查看全部评分

2 个回复

倒序浏览
值得学习ing!
回复 使用道具 举报

同学给你加分了!好好学习!蔡蔡和叶叶在黑马等着你!黑马有你更精彩!快点为了当土豪努力吧!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马