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);

}

相关推荐
老神在在0012 分钟前
java网络原理5
java·开发语言·网络·学习
__lost8 分钟前
MATLAB画一把伞
开发语言·matlab·模拟一把伞
web Rookie24 分钟前
09 Python字典揭秘:数据的高效存储
开发语言·python
古月฿28 分钟前
流水线问题(算法设计)C++
开发语言·c++·算法
运维@小兵33 分钟前
SpringBoot使用分组校验解决同一个实体对象在不同场景下需要不同校验规则的问题
java·spring boot·后端
计算机毕设定制辅导-无忧学长1 小时前
ActiveMQ 可靠性保障:消息确认与重发机制(一)
java·activemq·java-activemq
yy鹈鹕灌顶1 小时前
贪心算法精解(Java实现):从理论到实战
java·算法·贪心算法
Star abuse1 小时前
Python数据分析课程实验-1
开发语言·python·信息可视化
乔冠宇1 小时前
Java使用微信云服务HTTP API操作微信云开发数据库
java·http·微信
SimpleLearingAI2 小时前
Python协程入门指北
开发语言·python