当前位置: 首页 > news >正文

北京微信网站搭建费用桂林漓江自由行攻略

北京微信网站搭建费用,桂林漓江自由行攻略,邢台163信息港,国外做蛋糕的网站Convert数据类型转换类,从接触C#开始,就一直在用,这篇日志坐下深入的了解。 Convert类常用的类型转换方法 方法 说明 Convert.ToInt32() 转换为整型(int) Convert.ToChar() 转换为字符型(char) Convert.ToString() 转换为字符串型(st…

Convert数据类型转换类,从接触C#开始,就一直在用,这篇日志坐下深入的了解。

Convert类常用的类型转换方法

方法

说明

Convert.ToInt32()

转换为整型(int)

Convert.ToChar()

转换为字符型(char)

Convert.ToString()

转换为字符串型(string)

Convert.ToDateTime()

转换为日期型(datetime)

Convert.ToDouble()

转换为双精度浮点型(double)

Conert.ToSingle()

转换为单精度浮点型(float)

长类型(int、long)转换成短类型(byte、short)需要考虑容量的问题

转换为int型数据后进行了四舍五入的计算。
用convert类转换时注意数据表达方式的有效性,并不是任意类型之间都可以转换。比如string类型转int类型,string只能是数字。

示例:

int a = 1234;string asd = Convert.ToString(a);Console.WriteLine(asd);  // 1234Console.WriteLine(asd.GetType()); // System.stringchar qwe = Convert.ToChar(a);Console.WriteLine(qwe);  // ?  字符只能是一位Console.WriteLine(qwe.GetType());  // System.Charstring dc = "4125";int ws = Convert.ToInt32(dc);Console.WriteLine(ws);  // 4125Console.WriteLine(ws.GetType());  // System.Int32string dates = "2019-05-31"; // 必须是字符串才能转换成datetimeDateTime qa = Convert.ToDateTime(dates);Console.WriteLine(qa);  // 2019/5/31 0:00:00Console.WriteLine(qa.GetType()); // System.DateTimestring zxc = "5.32";double po = Convert.ToDouble(zxc);Console.WriteLine(po);  // 5.32Console.WriteLine(po.GetType());// System.Doublefloat rf = Convert.ToSingle(zxc);Console.WriteLine(rf);  // 5.32Console.WriteLine(rf.GetType());// System.single

Convert 和 Parse区别:

1.Convert.ToDouble与Double.Parse的区别。实际上Convert.ToDouble与 Double.Parse 较为类似,实际上 Convert.ToDouble内部调用了 Double.Parse:(1)对于参数为null的时候:

Convert.ToDouble参数为 null 时,返回 0.0; double wer = Convert.ToDouble(null);  // 0
Console.WriteLine(wer);

Double.Parse 参数为 null 时,抛出异常。 

double ed = Double.Parse(null);   // 报错
Console.WriteLine(ed);


(2)对于参数为""的时候:
Convert.ToDouble参数为 "" 时,抛出异常; 

wer = Convert.ToDouble("");
Console.WriteLine(wer);

Double.Parse 参数为 "" 时,抛出异常。 

double ed = Double.Parse("");
Console.WriteLine(ed);

(3)其它区别:

Convert.ToDouble可以转换的类型较多; 
Double.Parse 只能转换数字类型的字符串。 
Double.TryParse 与 Double.Parse 又较为类似,但它不会产生异常,转换成功返回 true,转换失败返回 false。最后一个参数为输出值,如果转换失败,输出值为 0.0。

2.Convert.ToInt32()与int.Parse()的区别

(1)这两个方法的最大不同是它们对null值的处理方法: 
Convert.ToInt32(null)会返回0而不会产生任何异常,但int.Parse(null)则会产生异常。 

int cdf = Convert.ToInt32(null);  // 0
Console.WriteLine(cdf);
int pol = int.Parse(null);  // 报错
Console.WriteLine(pol);


(2)对数据进行四舍五入时候的区别
a. Convert.ToInt32(double value) 
如果 value 为两个整数中间的数字,则返回二者中的偶数;即 3.5转换为4,4.5 转换为 4,而 5.5 转换为 6。 不过4.6可以转换为5,4.4转换为4 

int ppp = Convert.ToInt32(5.5);  // 6
Console.WriteLine(ppp);


b. int.Parse("4.5") 
直接报错:"输入字符串的格式不正确". 

int aaa = int.Parse("4.5");  // 报错
Console.WriteLine(aaa);


c. int(4.6) = 4 
Int转化其他数值类型为Int时没有四舍五入,强制转换 

int lkj = (int)4.9;
Console.WriteLine(lkj);  // 4

(3)对被转换类型的区别
int.Parse是转换String为int 
Convert.ToInt32是转换继承自Object的对象为int的(可以有很多其它类型的数据). 
你得到一个object对象,你想把它转换为int,用int.Parse就不可以,要用Convert.ToInt32. 

object obj = new object();
obj = (object)4;
int aaaaa = Convert.ToInt32(obj);
Console.WriteLine(aaaaa);int bbbbb = int.Parse(obj);  // 这样写报错

测试使用全部代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Convertss
{class Program{static void Main(string[] args){int a = 1234;string asd = Convert.ToString(a);Console.WriteLine(asd);  // 1234Console.WriteLine(asd.GetType()); // System.stringchar qwe = Convert.ToChar(a);Console.WriteLine(qwe);  // ?  字符只能是一位Console.WriteLine(qwe.GetType());  // System.Charstring dc = "4125";int ws = Convert.ToInt32(dc);Console.WriteLine(ws);  // 4125Console.WriteLine(ws.GetType());  // System.Int32string dates = "2019-05-31"; // 必须是字符串才能转换成datetimeDateTime qa = Convert.ToDateTime(dates);Console.WriteLine(qa);  // 2019/5/31 0:00:00Console.WriteLine(qa.GetType()); // System.DateTimestring zxc = "5.32";double po = Convert.ToDouble(zxc);Console.WriteLine(po);  // 5.32Console.WriteLine(po.GetType());// System.Doublefloat rf = Convert.ToSingle(zxc);Console.WriteLine(rf);  // 5.32Console.WriteLine(rf.GetType());// System.singledouble wer = Convert.ToDouble(null);  // 0Console.WriteLine(wer);/*double ed = Double.Parse(null);   // 报错Console.WriteLine(ed);//*//* wer = Convert.ToDouble("");Console.WriteLine(wer);double ed = Double.Parse("");Console.WriteLine(ed);//*/int cdf = Convert.ToInt32(null);  // 0Console.WriteLine(cdf);/*int pol = int.Parse(null);  // 报错Console.WriteLine(pol);//*/int ppp = Convert.ToInt32(5.5);  // 6Console.WriteLine(ppp);/*int aaa = int.Parse("4.5");  // 报错Console.WriteLine(aaa);//*/int lkj = (int)4.9;Console.WriteLine(lkj);  // 4object obj = new object();obj = (object)4;int aaaaa = Convert.ToInt32(obj);Console.WriteLine(aaaaa);// int bbbbb = int.Parse(obj);  // 这样写报错Console.ReadLine();}}
}

有好的建议,请在下方输入你的评论。

 

http://www.yayakq.cn/news/478699/

相关文章:

  • 顺企网官网电话搜索引擎优化排名关键字广告
  • 美容行业培训网站建设阿里巴巴网站建设的态度虚心
  • win7优化大师官方网站国外购物网站欣赏
  • 官方网站建设银行信用卡网站建设技术方案模板
  • 网站建设中++模板当今做哪个网站能致富
  • 网站需求怎么做怎么建公司官网
  • 网站服务器和空间的区别柳州做网站人员
  • 智能开关网站开发具体流程三台移动网站建设
  • 企业网站建设与管理试题网站建设 英文怎么说
  • 建站平台 在线提交表格网站排名下降怎么办
  • 陇南做网站网站建设哪个最好
  • 四川网站制作昆明网站建设报价
  • 云购物网站建设山东百度推广代理商
  • 读网站建设一定要买电脑实践吗陕西西乡网站建设
  • 对单位网站的要求wordpress已经上传图片加水印
  • 宁波网站推广优化公司莆田企业免费建站
  • 网站建设 图纸网溧水区城乡建设局网站
  • 如何做网站网站的教程随意设计一个网站
  • 百度收录最新方法seo工具
  • 掘金网站建设被官方认可赚钱软件
  • 织梦网站更改主页链接网站代运营公司排名
  • 如何看一个网站是用哪个语言做的商丘专业做网站公司
  • 设计型网站网站地址栏小图标
  • PHP开源网站开发系统仿网站制作教学视频教程
  • 泉州晋江网站建设费用做网站申请什么商标
  • 成都 网站建设 app 开发怎么做网站然后卖出去
  • 哲学专业特色建设网站wordpress主题汉化
  • 手机能建网站吗网站建设 兼职
  • 许昌哪里做网站wordpress评论框
  • 国内哪些网站是php做的信息流广告优秀案例