自动类型转换(由系统自动完成)
不会导致数据精度的丢失
只能从底精度类型转高精度类型
强制类型转换 会丢失精度需要显示的转换 1.使用类型进行强制类型转换 直接舍去小数部分不进行四舍五入
·
2.使用系统提供的强制转换方法进行转换 会四舍五入了
特殊的float
3.使用类型的解析方法(专门对字符串操作)Parse
using System; namespace Lesson07 { class MainClass { public static void Main (string[] args) { /* 自动类型转换 */ // 只能由低精度向高精度转换 int a = 3;// 整数部分 float b = 4.5f;// 整数部分+小数部分 b = a;// 自动将int类型的a ,转换成float 类型的b,然后进行赋值 Console.WriteLine (b); // byte<short<int<long short c= 5; a = c; /* 强制类型转换 */ // 会丢失精度需要显示的转换 // 使用类型进行强制类型转换 直接舍去小数部分不进行四舍五入 int i = 5; float j = 14.6f; i = (int)j; Console.WriteLine (i); // 使用系统提供的强制转换方法进行转换 会四舍五入了 int x= 6; float y= 9.7f; // Convert - 强制 // Int32 32位整数类型() x=Convert.ToInt32(y); short z = Convert.ToInt16 (y); Console.WriteLine (z); Console.WriteLine (x); int r = 4; //Single 单精度浮点型 float t=Convert.ToSingle(r); double dl = Convert.ToDouble (r); /* 使用类型的解析方法(专门对字符串操作)Parse */ string str= "123"; int v = Convert.ToInt32(str); Console.WriteLine (v); int v2 = int.Parse (str); Console.WriteLine (v2); string s= "5.6"; float ss = float.Parse (s); Console.WriteLine (ss); } } }
链接http://edu.csdn.net/course/detail/1982/30923?auto_start=1