`
zheyiw
  • 浏览: 997434 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

C#工具类Util

    博客分类:
  • C#
阅读更多

//小工具
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] + "";
        }
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics