您的位置:首页 > 移动开发 > Unity3D

Unity开发基础——类型转换学习笔记

2016-08-10 13:13 567 查看
Unity开发基础——使用字符串学习笔记

类型转换包括:自动转换+强制转换

 


一、自动转换

1、自动转换:由系统自动完成,不会导致数据精度丢失,只能从低精度类型转换高精度类型。

using System;

namespace Lesson07
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            //自动类型转换
            //只能由低精度类型高精度类型转换
            int a = 3;//整数部分
            float b = 4.6f;//整数,小数部分
            b = a;//自动将int类型变量a的值转换成float类型,然后进行赋值。

            Console.WriteLine (b);
        }
    }


2、精度由低到高   byte < short < int < long

 

只要是由低精度类型到高精度类型,都可以由系统自动转换,我们可以不参与!

 

using System;

namespace Lesson07
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            //自动类型转换
            //只能由低精度类型高精度类型转换
            int a = 3;//整数部分
            float b = 4.6f;//整数,小数部分
            b = a;//自动将int类型变量a的值转换成float类型,然后进行赋值。

            Console.WriteLine (b);

            // 精度由低到高   byte < short < int < long
            short c=5;
            a = c;
            Console.WriteLine (a);

        }
    }
}



3、如果反过来把int类型转为short类型,系统会自动报错!



因为系统无法自动把高精度类型转换成低精度类型,这时候如果要转换,就要我们人工干预,这时候就要强制类型转换。

二、强制转换

强制转换:从高精度转向低精度类型需要强制转换,会丢失精度,需要显式地进行转换。



1、使用类型进行转换,会直接舍去小数部分

using System;

namespace Lesson07
{
    class MainClass
    {
        public static void Main (string[] args)
        {

            // 1 .使用类型进行转换,会直接舍去小数部分
            int i=5;
            float j = 15.6f;
            i = (int)j;
            Console.WriteLine (i);

            short d = 4;
            d = (short)i;
            Console.WriteLine (d);

        }
    }
}



      2、使用系统提供的强制转换方式进行转换,Convert _强制转换会进行四舍五入。

  // 2 .使用系统提供的强制转换方式进行转换,会进行四舍五入。
          using System;

namespace Lesson07
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            // 2 .使用系统提供的强制转换方式进行转换,会进行四舍五入。
            int x=6;
            float y = 9.7f;
            x = Convert.ToInt32 (y);//10
            Console.WriteLine (x);

            y = 9.0f;
            x = Convert.ToInt32 (y);//9
            Console.WriteLine (x);

        }
    }
}

注释说明:

Convert _强制转换

int32 _32位整数类型(int) 

int16 _16位整数类型(short) 

 

举例:

            int x=6;
            float y = 9.7f;
            //Convert _强制转换
            //int32 _32位整数类型(int)
            x = Convert.ToInt32 (y); //10

            //int16 _16位整数类型(short)
            short  z=Convert.ToInt16(y); //10

            Console.WriteLine (x);
            Console.WriteLine (z);

 


针对于Convert后面的转换方式,大家可以多多练习!

 

特殊情况:single_单精度浮点型

            int r = 4;
            //single_单精度浮点型
            float t = Convert.ToSingle (r);
            Console.WriteLine (t); 

 

3 .使用类型的解析方法进行转换,Parse_解析只能把字符串转成整数或者浮点数。

            在使用解析方法之前,我们正常的情况下,把数字字符串转成int类型,使用的方式如下:

            string str="123";
            int v = Convert.ToInt32 (str);
            Console.WriteLine (v);

//3 .使用类型的解析方法进行转换,Parse_解析只能把字符串转成整数或者浮点数。
            string str="123";
            //Parse_解析
            int v = int.Parse (str);//123
            v++;//124
            Console.WriteLine (v);

            string s = "5.6";
            float ss = float.Parse (s);
            Console.WriteLine (ss); 

在通过在CSDN学院搜索"宋晓波"观看C#入门基础课程

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息