C# —— 显示转换

显示转换: 通过一些方法可以将其他数据类型转换为我们想要的数据类型

1.括号强转

作用: 一般情况下 将高精度的类型转换为低精度
            // 语法: 变量类型 变量名 = (转换的变量类型名称) 变量;
            // 注意: 精度问题 范围问题

sbyte sb = 1;
short s = 1;
int i = 10;
long l = -1;

强转时 可能会出现范围问题 需要注意

 s = (short)i;
 sb = (sbyte)l;
 Console.WriteLine(s);
 Console.WriteLine(sb);

无符号

byte b = 5;
ushort us = 6;
uint ui = 10;
ulong ul = 10;

b = (byte)ul;
Console.WriteLine(b);

不同类型 无符号和有符号强转
            // 注意 强转时一定要注意范围 不然得到的结果会异常

 b = (byte)l;
 Console.WriteLine(b);
 // 浮点数
 decimal dl = 0.6m;
 double d = 2.8;
 float f = 0.5f;

 // 浮点数之间强转 : 同整数之间相同
 dl = (decimal)d;
 Console.WriteLine(dl);

 // 整形和浮点数进行转换
 // 浮点数强转为整形时 会直接省略掉小数点后面的小数
 i = (int)dl;
 Console.WriteLine("这是强转decimal:" + i);
 i = (int)d;
 Console.WriteLine("这是强转double:" + i);
 i = (int)f;
 Console.WriteLine("这是强转float:" + i);

 字符类型 : 可以将数字强转为char类型(有这个ASCII码)

int A = 'a';
Console.WriteLine(A); 
char c = (char)A;
Console.WriteLine(c); 
// 布尔类型 : 无法进行强转
bool t = true;
// t = (float)t;
// Console.WriteLine(t);
// string 类型 : 无法进行强转
string str = "123";
// str = (int)str;
// 布尔类型和string类型 是无法进行强转的

2.Parse法

作用: 将字符串类型转换为对应的类型
            // 语法: 变量类型.parse(字符串);
            // 注意: 字符串必须能够转换为相应类型 否则报错

string str2 = "1254536";
i = int.Parse(str2);
Console.WriteLine(i);

我们转换字符串 必须是要能够转成对应类型的字符 否则会报错
            // 转换时 其实也遵循隐式转换原则 大范围装小范围
            // i = int.Parse("123.45");

 string str3 = "123.45";
 float f2 = float.Parse(str3);
 Console.WriteLine(f2);
 string str7 = "123";
 float f3 = float.Parse(str7);
 Console.WriteLine("直接使用float去转换整数字符串"+ f3);

string str4 = "15645666";
// byte b4 = byte.Parse(str4);
// Console.WriteLine(b4);
string str5 = "1";
// 有符号
long l5 = long.Parse(str5);
int i5 = int.Parse(str5);
short s5 = short.Parse(str5);
sbyte sb5 = sbyte.Parse(str5);
// 无符号
ulong ul5 = ulong.Parse(str5);
uint ui5 = uint.Parse(str5);
ushort us5 = ushort.Parse(str5);
byte b5 = byte.Parse(str5);
// 浮点数
string str6 = "123.5";
float f5 = float.Parse(str6);
double d5 = double.Parse(str6);
decimal dl5 = decimal.Parse(str6);

// 布尔值
bool bl5 = bool.Parse("true");
bool bl6 = bool.Parse("false");
Console.WriteLine(bl5);
Console.WriteLine(bl6);
// 字符
char c6 = char.Parse("A");
Console.WriteLine(c6);

3.Convert法 

作用: 更加准确的将各个类型之间进行相互转换
            // 用法: Convert.To目标类型(变量或常量)
            // 注意: 需要转换的变量或者常量必须正确 否则会报错

每一个类型都存在一个对应的convert.to方法
           

// 有符号
long lg = Convert.ToInt64(str_one);
int it = Convert.ToInt32(str_one);
short st = Convert.ToInt16(str_one);
sbyte sbt = Convert.ToSByte(str_one);

// 无符号
ulong ulg = Convert.ToUInt64(str_one);
uint uit = Convert.ToUInt32(str_one);
ushort ust = Convert.ToUInt16(str_one);
byte bt = Convert.ToByte(str_one);
// 浮点数
float ft = Convert.ToSingle(str_one);
double db = Convert.ToDouble(str_one);
decimal dc = Convert.ToDecimal(str_one);
Console.WriteLine(lg);
Console.WriteLine(ft);
Console.WriteLine(db);
Console.WriteLine(dc);
Console.WriteLine("************************************");

convert 方法  精度更加准确
            // 精度比括号强转会更好一点 会进行四舍五入

int num_two = Convert.ToInt32(1.2456f); // 1
Console.WriteLine(num_two);
num_two = Convert.ToInt32(1.5456f); // 2
Console.WriteLine(num_two);
 Console.WriteLine("************************************");

如果把字符串转换为对应类型 那么字符串一定要符合类型  否则会报错
            // convert.to 去转换数据类型时  也遵循隐式转换原则 大范围装小范围
            // num_two = Convert.ToInt32("1.5456"); // 报错
            // Console.WriteLine(num_two);

 float float_two = Convert.ToSingle("1.5456");
 Console.WriteLine(float_two);
 float float_three = Convert.ToSingle("5");
 Console.WriteLine(float_three);

特殊类型转换

// bool类型也可以转换为整形 true 为 1 false 为 0
bool bl1 = true;
bool bl2 = false;
num_two = Convert.ToInt32(bl1);
Console.WriteLine(num_two); // 1
num_two = Convert.ToInt32(bl2);
Console.WriteLine(num_two); // 0
// 布尔值转字符串
// 数值转换为bool类型
// 除了布尔值字符串 其余字符串转换布尔值会报错
// 除零外 其余数字类型转换为布尔值时为true 0 为false
bool bl3 = Convert.ToBoolean("true");
// bool bl4 = Convert.ToBoolean("123");
bool bl4 = Convert.ToBoolean(-5);
bool bl8 = Convert.ToBoolean(2.5);
Console.WriteLine(bl3);
Console.WriteLine(bl4);
Console.WriteLine(bl8);

Console.WriteLine("**********************************");
// char  类型
char B = 'B';
num_two = Convert.ToInt32(B);
Console.WriteLine(num_two);
// 字符串转 char 类型
B = Convert.ToChar("王");
Console.WriteLine(B);
// 数字转 char类型 是根据对应的ASCII码表进行转换
B = Convert.ToChar(66);
Console.WriteLine(B);

Console.WriteLine("*********string类型**************");
// string 类型
string str_five = Convert.ToString(num_two);
Console.WriteLine(str_five);

4.其他类型转string

 ToString()
            // 作用: 拼接字符
            // 用法: 变量或者常量.ToString();

string str_six = 5.ToString();
Console.WriteLine(str_six);
str_six = true.ToString();
Console.WriteLine(str_six);
str_six = 1.2f.ToString();
Console.WriteLine(str_six);
str_six = 'A'.ToString();
Console.WriteLine(str_six);

字符串拼接时  其实 默认自动调用的ToString方法 装换为string类型

Console.WriteLine("每个人都有" + 10 + "个手指头" + true);

 

相关推荐

  1. C# —— 显示转换

    2024-06-11 23:30:05       35 阅读
  2. 显示隐式-》初始化 & 类型转换

    2024-06-11 23:30:05       25 阅读
  3. C++ 强制转换

    2024-06-11 23:30:05       57 阅读
  4. c语言大小写转换

    2024-06-11 23:30:05       46 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-11 23:30:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-11 23:30:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-11 23:30:05       87 阅读
  4. Python语言-面向对象

    2024-06-11 23:30:05       96 阅读

热门阅读

  1. springboot接收byte[]字节

    2024-06-11 23:30:05       28 阅读
  2. 深度学习在老年痴呆检测中的应用:数据集综述

    2024-06-11 23:30:05       34 阅读
  3. C语言学习第四天

    2024-06-11 23:30:05       34 阅读
  4. shell脚本

    2024-06-11 23:30:05       30 阅读
  5. c++_0基础_讲解2 头文件 基本框架

    2024-06-11 23:30:05       36 阅读
  6. C++习题精选(4)—— 栈

    2024-06-11 23:30:05       35 阅读
  7. C++ Compound types overview

    2024-06-11 23:30:05       24 阅读