asp.net ToString格式大全与Convert.ToString区别 (1/2)

asp教程.net tostring格式大全与convert.tostring区别

通常object到string有四种方式:(假设有object obj)obj.tostring,convert.tostring,(string)obj,obj as string。
他们都能将object对象转换成string对象。我就讲讲他们的异同以及在实际中应该使用哪个。

前两个方法通常是由别的对象得到string对象,它们间的区别只表现在要转换的对象为null时,如果obj为null,调用obj.tostring方法会导致nullreferenceexception异常,调用convert.tostring不会抛出异常而返回一个null。
用强制转换(string)obj要求obj的运行时类型必须是string。如果不是,就会抛出异常。用as方法则会相对平稳,当obj的运行时类型不是string时会返回null而不抛出异常。

所以在通常在我们需要得到某个对象的string表达形式时,我们应该使用tostring和convert.tostring,这时候你就得根据情形选一个,假如你能保证你的对象不为null,则两个差不多。如果有可能为null,你就应该用convert.tostring,如果你希望它为null的时候抛出异常,那么当然可以选择.tostring。

字符型转换为字符串

// c 货币

2.5.tostring("c"); // ¥2.50

// d 10进制数

25.tostring("d5"); // 25000

// e 科学型

25000.tostring("e"); // 2.500000e+005

// f 固定点

25.tostring("f2"); // 25.00

// g 常规

2.5.tostring("g"); // 2.5

// n 数字

2500000.tostring("n"); // 2,500,000.00

// x 16进制

255.tostring("x"); // ff

// c# 日期格式
datetime dt = datetime.now;
 
dt.tostring();//2005-11-5 13:21:25

dt.tofiletime().tostring();//127756416859912816

dt.tofiletimeutc().tostring();//127756704859912816

dt.tolocaltime().tostring();//2005-11-5 21:21:25

dt.tolongdatestring().tostring();//2005年11月5日

dt.tolongtimestring().tostring();//13:21:25

dt.tooadate().tostring();//38661.5565508218

dt.toshortdatestring().tostring();//2005-11-5

dt.toshorttimestring().tostring();//13:21

dt.touniversaltime().tostring();//2005-11-5 5:21:25

dt.year.tostring();//2005

dt.date.tostring();//2005-11-5 0:00:00

dt.dayofweek.tostring();//saturday

dt.dayofyear.tostring();//309

dt.hour.tostring();//13

dt.millisecond.tostring();//441

dt.minute.tostring();//30

dt.month.tostring();//11

dt.second.tostring();//28

dt.ticks.tostring();//632667942284412864

dt.timeofday.tostring();//13:30:28.4412864

dt.tostring();//2005-11-5 13:47:04

dt.addyears(1).tostring();//2006-11-5 13:47:04

dt.adddays(1.1).tostring();//2005-11-6 16:11:04

dt.addhours(1.1).tostring();//2005-11-5 14:53:04

dt.addmilliseconds(1.1).tostring();//2005-11-5 13:47:04

dt.addmonths(1).tostring();//2005-12-5 13:47:04

dt.addseconds(1.1).tostring();//2005-11-5 13:47:05

dt.addminutes(1.1).tostring();//2005-11-5 13:48:10

dt.addticks(1000).tostring();//2005-11-5 13:47:04

dt.compareto(dt).tostring();//0

dt.add(?).tostring();//问号为一个时间段

dt.equals("2005-11-6 16:11:04").tostring();//false

dt.equals(dt).tostring();//true

dt.gethashcode().tostring();//1474088234

dt.gettype().tostring();//system.datetime

dt.gettypecode().tostring();//datetime

   

首页 1 2 末页

时间: 2024-07-29 19:12:56

asp.net ToString格式大全与Convert.ToString区别 (1/2)的相关文章

C#.ToString()格式大全

原文:C#.ToString()格式大全 C#.ToString()格式大全     stringstr1=string.Format("{0:N1}",56789);               //result: 56,789.0 stringstr2=string.Format("{0:N2}",56789);               //result: 56,789.00 stringstr3=string.Format("{0:N3}&quo

ToString格式大全

C   货币 2.5.ToString("C") ¥2.50 D   十进制数 25.ToString("D5") 00025 E 科学型 25000.ToString("E") 2.500000E+005 F 固定点 25.ToString("F2") 25.00 G 常规 2.5.ToString("G") 2.5 N 数字 2500000.ToString("N") 2,500,0

asp.net ToString()格式设置大全_实用技巧

C 货币 2.5.ToString("C") ¥2.50 D 十进制数 25.ToString("D5") 00025 E 科学型 25000.ToString("E") 2.500000E+005 F 固定点 25.ToString("F2") 25.00 G 常规 2.5.ToString("G") 2.5 N 数字 2500000.ToString("N") 2,500,000.0

浅谈C#中ToString()和Convert.ToString()的区别_C#教程

浅谈ToString()和Convert.ToString()方法的区别 一.一般用法说明 ToString()是Object的扩展方法,所以都有ToString()方法;而Convert.ToString(param)(其中param参数的数据类型可以是各种基本数据类型,也可以是bool或object类对象. 二.ToString()和Convert.ToString()的区别 一般情况下,这两种方法都可以通用,但是当返回的数据类型中有可能出现null值时如果调用ToString方法了,就会返

DateTime时间格式大全

  在c# / ASP.net中我们可以通过使用DataTime这个类来获取当前的时间.通过调用类中的各种方法我们可以获取不同的时间:如:日期(2008-09-04).时间(12:12:12).日期+时间(2008-09-04 12:11:10)等.    //获取日期+时间  DateTime.Now.ToString();            // 2008-9-4 20:02:10  DateTime.Now.ToLocalTime().ToString();        // 200

word2007设置表格格式大全

  word2007设置表格格式大全         Word2007使用"表格样式"设置整个表格的格式 创建表格后,可以使用"表格样式"来设置整个表格的格式.将指针停留在每个预先设置好格式的表格样式上,可以预览表格的外观. 1.在要设置格式的表格内单击. 2.在"表格工具"下,单击"设计"选项卡. 3.在"表格样式"组中,将指针停留在每个表格样式上,直至找到要使用的样式为止. 注释:要查看更多样式,请单击

简历表格下载word格式大全

  简历是求职者给招聘单位发的一份简要介绍.包含自己的基本信息:姓名.性别.年龄.民族.籍贯.政治面貌.学历.联系方式,以及自我评价.工作经历.学习经历.荣誉与成就.求职愿望.对这份工作的简要理解等.现在一般找工作都是在通过网络来找,因此一份良好的个人简历对于获得面试机会至关重要.简历表格下载word格式大全,一起来学习一下吧. 为大家整理了三份具有代表性的<简历表格下载word格式>的简历模板给大家参考,喜欢的朋友赶紧收藏吧! 简历表格下载word格式一 姓名  xx 性别 男  出生年月

xmlhtp-微信支付:用ASP把xml格式post到https,出现【指定资源下载失败】

问题描述 微信支付:用ASP把xml格式post到https,出现[指定资源下载失败] strxml="<xml>" strxml=strxml&"<appid>wxb4df923b092389cf</appid>" strxml=strxml&"<attach>487396</attach>" strxml=strxml&"<body>12

如何用ASP发送html格式的邮件?_编程10000问

如何用ASP发送html格式的邮件? <% Dim objMail Set objMail = CreateObject("CDONTS.Newmail") objMail.TO = "sunchunliang@263.net" objMail.From =shenzhaoyang@intels.net objmail.Subject = "闪亮日子之html撼雪喷云" objMail.MailFormat = cdoMailFormatM