【C#】类型转换-显式转换:括号强转、Parse法、Convert法、其他类型转string
目录
(图片来源网络,侵删)
一、括号强转
1.有符号整型
2.无符号整型
3.浮点之间
4.无符号和有符号
5.浮点和整型
(图片来源网络,侵删)
6.char和数值类型
7.bool和string是不能够通过 括号强转的
二、Parse法
1.有符号
2.无符号
3.浮点型
(图片来源网络,侵删)
4.特殊类型
三、Convert法
1.转字符串
2.转浮点型
3.特殊类型转换
4.每一个类型都存在对应的 Convert中的方法
四、其他类型转string
1.所有其他类型的数据
2.当我们进行字符串拼接时,就自动会调用 tostring 转成string
一、括号强转
主要用于数值之间,低精度转高精度,如byte转为int。
- 作用:一般情况下 将高精度的类型强制转换为低精度
- 语法:变量类型 变量名=(变量类型)变量;
- 注意:精度问题 范围问题
1.有符号整型
short sb = 1; short s = 1; int i = 1; long l = 1; s = (short)i; Console.WriteLine(s); i = (int)1; sb = (sbyte)s; sb = (sbyte)i; sb = (sbyte)l;
2.无符号整型
byte b = 1; uint ui = 1; b = (byte)ui;
3.浮点之间
float f = 1.1f; double d = 1.1234567890123456789f; f = (float)d; Console.WriteLine(f);
4.无符号和有符号
uint ui2 = 1; int i2 = 1; //在强转时 一定要注意范围 不然得到的结构 可能有异常 ui2 = (uint)i2; Console.WriteLine(ui2); i2 = (int)ui2;
5.浮点和整型
//浮点数强转成整型时 会直接抛弃掉小数点后面的小数 i2 = (int)1.24f; Console.WriteLine(i2);
6.char和数值类型
i2 = 'A'; char c = (char)i2; Console.WriteLine(c);
7.bool和string是不能够通过 括号强转的
//bool bo = true; //int i3 = (bool)bo; //string str="123"; //i3=(int)str;
:所有和数值有关的都支持低精度的去存储高精度的(用括号强转的方式)
二、Parse法
主要用来把字符串转为数值。
- 作用:把字符串转换为对应的类型
- 语法:变量类型.Parse("字符串)
- 注意:字符串必须能够转换成对应类型(合法合规)否则报错
1.有符号
int i4 = int.Parse("123"); Console.WriteLine(i4); //我们填写字符串 必须是要能够转换成对应类型的字符串,如果不合规范,报错 //i4 = int.Parse("123.45");// //值得范围 必须是能够倍变量存储的值 否则报错 //short s3 = short.Parse("40000"); //console.writeLine(s3);
2.无符号
//无符号 Console.WriteLine(byte.Parse("1")); Console.WriteLine(ushort.Parse("1")); Console.WriteLine(ulong.Parse("1")); Console.WriteLine(uint.Parse("1"));
3.浮点型
//浮点型 float f3 = float.Parse("1.2323"); double d3 = double.Parse("1.2323");
4.特殊类型
//bool和char类型转化为string类型 bool b5 = bool.Parse("true"); Console.WriteLine(b5); char c2 = char.Parse("A"); Console.WriteLine(c2);
:Parse法用来将string字符串类型转换为对应的类型,要注意的是:我们填写的字符串类型必须是合法合规的,不然会报错
三、Convert法
更全面的转换方式,精度更高,可以把各种类型的数据转为各种类型
- 作用 更具体的将 各个类型之间相互转换
- 语法:Convert.To目标类型(变量或常量)
- 注意:填写的变量或常量必须正确 否则出错
1.转字符串
如果是把字符串转成对应类型 那字符串一定要合法合规
int a = Convert.ToInt32("12"); Console.WriteLine(a);
2.转浮点型
// 精度比括号强转好一点,会四舍五入 a = Convert.ToInt32(1.65845f); Console.WriteLine(a);//2
3.特殊类型转换
//把bool类型也可以转成 数值类型 true对应1 false对应0 a = Convert.ToInt32(true); Console.WriteLine(a); a = Convert.ToInt32(false); Console.WriteLine(a); a = Convert.ToInt32('A'); Console.WriteLine(a)
4.每一个类型都存在对应的 Convert中的方法
//每一个类型都存在对应的 Convert中的方法 sbyte sb5 = Convert.ToSByte("1"); short s5 = Convert.ToInt16("1"); int i5 = Convert.ToInt32("1"); long l5 = Convert.ToInt64("1"); byte b6 = Convert.ToByte("1"); ushort us5 = Convert.ToUInt16("1"); uint ui5 = Convert.ToUInt32("1"); ulong ul5 = Convert.ToUInt64("1"); float f5 = Convert.ToSingle("13.2"); double d5 = Convert.ToDouble("13.2"); decimal de5 = Convert.ToDecimal("13.2"); bool bo5 = Convert.ToBoolean("true"); char c5 = Convert.ToChar("A"); string str5 = Convert.ToString(123123); Console.WriteLine(sb5); Console.WriteLine(i5); Console.WriteLine(l5); Console.WriteLine(b6); Console.WriteLine(us5); Console.WriteLine(ui5); Console.WriteLine(ul5); Console.WriteLine(f5); Console.WriteLine(d5); Console.WriteLine(de5); Console.WriteLine(bo5); Console.WriteLine(c5); Console.WriteLine(str5);
四、其他类型转string
可以把所有其他类型的数据通过.string方式转换为string类型
- 作用:拼接打印
- 语法:变量.tostring();
1.所有其他类型的数据
string str6 = 1.ToString(); Console.WriteLine(str6); str6 = true.ToString(); Console.WriteLine(str6); str6 = 'A'.ToString(); Console.WriteLine(str6); str6 = 1.2f.ToString(); Console.WriteLine(str6); int aa = 1; str6 = aa.ToString(); Console.WriteLine(aa); bool bo6 = true; str6 = bo6.ToString(); Console.WriteLine(bo6);
2.当我们进行字符串拼接时,就自动会调用 tostring 转成string
Console.WriteLine("123123" + 1 + true); str6 = "123123" + 1 + true + 1.23;
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。
还没有评论,来说两句吧...