const double PAI= 3.14;//常量 PAI = 3;
枚举:
internal class Program { static void Main(string[] args) { Gender gender =Gender.Male; Console.WriteLine(gender); Console.WriteLine((int)gender);//强制转换成int类型 Console.ReadLine(); } } /// <summary> /// 枚举 /// </summary> public enum Gender {/// <summary> /// 男 /// </summary> Male=1, /// <summary> /// 女 /// </summary> Female=0 }
强制转换:
值转换:
/// <summary> /// 值转换 /// </summary> public static void mothed() { double a = 10.44; Console.WriteLine(a); Console.WriteLine((int)a);//丢失精度 int b = 16; Console.WriteLine((double)b); object c = 10.55; //Console.WriteLine((int)c);//出错 //object为引用类型,所以强制转换成int类型,要对应不能有小数,或者先转到对应类型 Console.WriteLine((int)(double)c);//c是double所以先转double,再int Console.ReadLine(); }
Parse转换:
/// <summary> /// Parse转换,字符串->值类型 /// </summary> public static void mothed1() { string text = "234.567891230"; //Console.WriteLine(int.Parse(text));//必须是对应的类型 Console.WriteLine(double.Parse(text));//double精确到小数点后15位 Console.WriteLine(float.Parse(text));//float精确到小数点后6位 Console.ReadLine(); }
Convert.To转换:(万能,任意都可以转换)
public static void mothed2() { string test = "123.590"; string test1 = "123"; int a = Convert.ToInt32(test1);//格式要对应 double b=Convert.ToDouble(test); Console.WriteLine(a); Console.WriteLine(b); double text2 = 13.57; int c=Convert.ToInt32(text2);//四舍五入 Console.WriteLine(c); double text3 = 13.49;//舍去 int d = Convert.ToInt32(text3); Console.WriteLine(d); //特殊 double text4 = 17.50;//奇数.50进位 double text5 = 18.50;//偶数.50舍去 int e=Convert.ToInt32(text4); int f=Convert.ToInt32(text5); Console.WriteLine(e); Console.WriteLine(f); Console.ReadLine(); }
标签:运输,Convert,Console,C#,double,转化,int,WriteLine,ToInt32 From: https://www.cnblogs.com/lin-07/p/17373949.html