c#各种对象转换

1、转换为String

public static string ConvertString(this Object obj)

{

if (obj == null || obj == System.DBNull.Value)

return string.Empty;

return obj.ToString();

}

2、转换为double

public static double ConvertDouble(this Object obj)

{

double db = 0;

if (obj == null || obj == System.DBNull.Value)

return db;

bool bol = double.TryParse(obj.ToString(), out db);

if (bol)

return db;

else

return 0;

}

3、转换为int

public static int ConvertInt(this Object obj)

{

int m_int = 0;

if (obj == null || obj == System.DBNull.Value)

return m_int;

bool bol = Int32.TryParse(obj.ToString(), out m_int);

if (bol)

return m_int;

else

return 0;

}

4、转换为日期

public static DateTime ConvertDate(this Object obj)

{

DateTime m_date = DateTime.MinValue;

if (obj == null || obj == System.DBNull.Value)

return m_date;

bool bol = DateTime.TryParse(obj.ToString(), out m_date);

if (bol)

return m_date;

else

return DateTime.MinValue;

}

5、转换为时间

public static DateTime ConvertDateTime(this string str)

{

DateItem di = new DateItem();

string date = di.ConvertDateToData(str, "-");

return DateTime.Parse(date);

}

相关推荐
冷雨夜中漫步7 小时前
Python快速入门(6)——for/if/while语句
开发语言·经验分享·笔记·python
JH30738 小时前
SpringBoot 优雅处理金额格式化:拦截器+自定义注解方案
java·spring boot·spring
暖馒9 小时前
Modbus应用层协议的深度剖析
网络·网络协议·c#·wpf·智能硬件
m0_736919109 小时前
C++代码风格检查工具
开发语言·c++·算法
Coder_Boy_9 小时前
技术让开发更轻松的底层矛盾
java·大数据·数据库·人工智能·深度学习
2501_944934739 小时前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
invicinble10 小时前
对tomcat的提供的功能与底层拓扑结构与实现机制的理解
java·tomcat
较真的菜鸟10 小时前
使用ASM和agent监控属性变化
java
黎雁·泠崖10 小时前
【魔法森林冒险】5/14 Allen类(三):任务进度与状态管理
java·开发语言
2301_7634724611 小时前
C++20概念(Concepts)入门指南
开发语言·c++·算法