C#之数据类型转换

来源:互联网 发布:对冲基金编程 编辑:程序博客网 时间:2024/05/17 10:57
问题描述:输入的字符串转化为其他数据类型
//从控制台接受的字符(char)转换为int 类型数据
public static void Main (string[] args)
        {
            int a;
            a = 5;
            float b = 5.6f;
            //系统自动的类型转换,只能由低精度到高精度
            b = a;
            Console.WriteLine (b);
            //强制类型转换
            //1.使用类型进行转换,会直接舍弃小数部分

            int i=5;
            float j = 7.6f;
            i = (int)j;
            Console.WriteLine (i);
            //
            //2. 使用系通提供的强制转换方法
            int x=6;
            float y = 10.7f;
            x = Convert.ToInt32 (y);
            Console.WriteLine (x);
            //3.使用类型的解析方法进行转换
            string str="123";
            //int v = Convert.ToInt32 (str);
            int v=int.Parse(str);
            Console.WriteLine (v);
        }