//小工具
public class Util
{
//将不同类型的值格式化成字符串输出
public static string FormatDBdata(object item)
{
try
{
if (item.GetType() == typeof(decimal))
{
return Convert.ToString(item);
}
if (item.GetType() == typeof(DateTime))
{
return string.Format("{0:yyyy-MM-dd HH:mm:ss}", item);
}
return Convert.ToString(item);
}
catch (Exception ex)
{
throw ex;
}
}
//求浮点型数小数,去掉小数位最后面的0
public static decimal ToDecimal(object obj)
{
if (obj + "" != "")
{
try
{
decimal dm = Convert.ToDecimal(obj);
string str = dm.ToString("#.#########");//去除小数点后的0
if (str == "")
{
str = "0";
}
dm = decimal.Parse(str);
return dm;
}
catch
{
return 0.0m;
}
}
return 0.0m;
}
//求浮点型数小数,小数位最后面的0去掉,并截取指定位数小数位
public static decimal ToDecimal(object obj, int digit)
{
return decimal.Round(ToDecimal(obj), digit, MidpointRounding.AwayFromZero);
}
//格式化到整数,不会报错
public static string ToIntStr(object var)
{
try
{
string value = var.ToString().Trim();
return Math.Floor(double.Parse(value)).ToString();
}
catch
{
return "0";
}
}
//格式化到整数,不会报错
public static int ToInt(object var)
{
try
{
return Convert.ToInt32(ToIntStr(var));
}
catch
{
return 0;
}
}
//IsNullOrWhiteSpace
public static bool isNull(string str)
{
if (string.IsNullOrWhiteSpace(str))
{
return true;
}
return false;
}
public static bool isNull(string[] arr)
{
if (null == arr || arr.Length <= 0)
{
return true;
}
return false;
}
public static bool isNull(List<object> list)
{
if (null == list || list.Count <= 0)
{
return true;
}
return false;
}
//表格对象为空,或者没有行
public static bool isNull(DataTable dt)
{
if (null == dt || dt.Rows.Count <= 0)
{
return true;
}
return false;
}
//数据集对象为空,或者没有没有,或者表中没有行
public static bool isNull(DataSet ds)
{
if (null == ds || null == ds.Tables || ds.Tables.Count <= 0 || isNull(ds.Tables[0]))
{
return true;
}
return false;
}
//返回当前时间,格式:yyyy-MM-dd HH:mm:ss
public static string TimeNow()
{
return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
public static string ToMD5(string str)
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
}
//获取表中0行0列的值
public static string GetFirst(DataTable dt)
{
if (isNull(dt))
{
return null;
}
else
{
return dt.Rows[0][0] + "";
}
}
//获取数据库中0表0行0列的值
public static string GetFirst(DataSet ds)
{
if (isNull(ds))
{
return null;
}
else
{
return ds.Tables[0].Rows[0][0] + "";
}
}
}
分享到:
相关推荐
C# Util 实用工具类 ,包含:Json、Net、Time、Compress、Config、Enums、File等等等常用使用工具类
C#常用工具类代码集合Util(自己工作总结),欢迎分享给大家,一起进步,有什么问题可以找我沟通,谢谢大家。
C#常用工具类代码集合Util第二版本(自己工作总结),包括常用工具类,扩展方法工具类,百度地图C#工具类,Echart工具类,Office工具类,Autofac工具类,Web开发常用工具类,Winform开发常用工具类,是自己工作十年...
整理了几个在DotNet2.0中C#开发常用的基础工具类,内容如下:StringUtil、ParseUtil、TypeUtil、ByteUtil、NumUtil、XmlUtil、RegexUtil、HashUtil、ColorUtil。
http请求工具类HttpClientUtil,get,post请求(csdn)————程序
BitVector CookieUtil DES FileUtil HttpUtil ImageFile JavaScriptString JsonHelper StringUtil Thumbnail Utils WebFileInfo
下面小编就为大家带来一篇HttpUtils 发送http请求工具类(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了C#字符串的常用操作工具类代码分享,需要的朋友可以参考下
sm4的java算法
本文实例讲述了C#实现的文件上传下载工具类。分享给大家供大家参考,具体如下: 这里给出的工具类是在VS2013环境下采用C#语言实现文件上传、下载功能。上传时,为避免文件名在服务器中重复,采用“服务器时间+8位...
XML与实体类的相互转换工具,util是工具类,MyTest包含实体类转xml字符串以及XML字符串转实体类的方法,需要引入外部依赖或jar包,亲测可用
(其中的服务端的类基本上使用了网络上一位朋友的共享)由于是整整三天的调查成果,因此例外拿个10分,请大家体谅。但我相信你不会失望的。 CMain展示了Server和Client端的Util用法,以java工程运行CMain可以进行...
DotNet.Utilities
这是本人多年积累的学习笔记,记录详细、内容宽广,对新手高手都会有所帮助 作者: 冯万里(Holemar) ..._util\c#\... c#常用函数 _util\ ... java常用函数 _util\js\... js常用函数 _util\python\... python常用函数
此类库由Fly经一年多在一些实际开发项目中运用到的公共方法,特于今日整合在一起已备...16.Util.cs 专门的一个工具类如发送邮件、获取Webconfig信息、获取本类库程序集的信息等 17.还有access、mysql、oracle的dbhelper
工程中会大量使用apache基金会的通用工具类,使代码更加简洁高效,在其提供的工具类没有的情况下或方法不全(如,对汉字的处理)的情况 增加/扩展某一工具类,一般xxxUtilsExt为文件后缀 随便提一下使用第三方工具类的...
dateutil类 日期、时间、星期操作类.已打jar包
有在LINQPad的的Util类新方法来加载/字符串或字节数组保存到一个中央计算机存储(查询之间共享)。这些方法Util.SaveString,Util.SaveBytes,Util.LoadString和Util.LoadBytes。 LINQPad的嵌入式ILSpy现在已经更新...
commons-discovery 提供工具来定位资源 (包括类) ,通过使用各种模式来映射服务/引用名称和资源名称。 commons-el 提供在JSP2.0规范中定义的EL表达式的解释器. commons-email 提供一组用于发送Email的API,它基于...
commons-discovery 提供工具来定位资源 (包括类) ,通过使用各种模式来映射服务/引用名称和资源名称。 commons-el 提供在JSP2.0规范中定义的EL表达式的解释器. commons-email 提供一组用于发送Email的API,它基于...