找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 61|回复: 0

[工具类] C#常用小工具类

[复制链接] IP属地:广东省广州市
发表于 2022-1-10 21:54:28 | 显示全部楼层 |阅读模式
  1. //小工具
  2.     public class Util
  3.     {
  4.         //将不同类型的值格式化成字符串输出
  5.         public static string FormatDBdata(object item)
  6.         {
  7.             try
  8.             {
  9.                 if (item.GetType() == typeof(decimal))
  10.                 {
  11.                     return Convert.ToString(item);
  12.                 }
  13.                 if (item.GetType() == typeof(DateTime))
  14.                 {
  15.                     return string.Format("{0:yyyy-MM-dd HH:mm:ss}", item);
  16.                 }
  17.                 return Convert.ToString(item);
  18.             }
  19.             catch (Exception ex)
  20.             {
  21.                 throw ex;
  22.             }
  23.         }    //求浮点型数小数,去掉小数位最后面的0
  24.         public static decimal ToDecimal(object obj)
  25.         {
  26.             if (obj + "" != "")
  27.             {
  28.                 try
  29.                 {
  30.                     decimal dm = Convert.ToDecimal(obj);
  31.                     string str = dm.ToString("#.#########");
  32.                     //去除小数点后的0
  33.                     if (str == "")
  34.                     {
  35.                         str = "0";
  36.                     }
  37.                     dm = decimal.Parse(str);
  38.                     return dm;
  39.                 }
  40.                 catch
  41.                 {
  42.                     return 0.0m;
  43.                 }
  44.             }
  45.             return 0.0m;
  46.         }
  47.         //求浮点型数小数,小数位最后面的0去掉,并截取指定位数小数位
  48.         public static decimal ToDecimal(object obj, int digit)
  49.         {
  50.             return decimal.Round(ToDecimal(obj), digit, MidpointRounding.AwayFromZero);
  51.         }    //格式化到整数,不会报错
  52.         public static string ToIntStr(object var)
  53.         {
  54.             try
  55.             {
  56.                 string value = var.ToString().Trim();
  57.                 return Math.Floor(double.Parse(value)).ToString();
  58.             }
  59.             catch
  60.             {
  61.                 return "0";
  62.             }
  63.         }
  64.         //格式化到整数,不会报错
  65.         public static int ToInt(object var)
  66.         {
  67.             try
  68.             {
  69.                 return Convert.ToInt32(ToIntStr(var));
  70.             }
  71.             catch
  72.             {
  73.                 return 0;
  74.             }
  75.         }
  76.         //IsNullOrWhiteSpace
  77.         public static bool isNull(string str)
  78.         {
  79.             if (string.IsNullOrWhiteSpace(str))
  80.             {
  81.                 return true;
  82.             }
  83.             return false;
  84.         }
  85.         public static bool isNull(string[] arr)
  86.         {
  87.             if (null == arr || arr.Length <= 0)
  88.             {
  89.                 return true;
  90.             }
  91.             return false;
  92.         }

  93.         public static bool isNull(List<object> list)
  94.         {
  95.             if (null == list || list.Count <= 0)
  96.             {
  97.                 return true;
  98.             }
  99.             return false;
  100.         }
  101.         //表格对象为空,或者没有行
  102.         public static bool isNull(DataTable dt)
  103.         {
  104.             if (null == dt || dt.Rows.Count <= 0)
  105.             {
  106.                 return true;
  107.             }
  108.             return false;
  109.         }
  110.         //数据集对象为空,或者没有没有,或者表中没有行
  111.         public static bool isNull(DataSet ds)
  112.         {
  113.             if (null == ds || null == ds.Tables || ds.Tables.Count <= 0 || isNull(ds.Tables[0]))
  114.             {
  115.                 return true;
  116.             }
  117.             return false;
  118.         }
  119.         //返回当前时间,格式:yyyy-MM-dd HH:mm:ss
  120.         public static string TimeNow()
  121.         {
  122.             return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  123.         }
  124.         public static string ToMD5(string str)
  125.         {
  126.             return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
  127.         }
  128.         //获取表中0行0列的值
  129.         public static string GetFirst(DataTable dt)
  130.         {
  131.             if (isNull(dt))
  132.             {
  133.                 return null;
  134.             }
  135.             else
  136.             {
  137.                 return dt.Rows[0][0] + "";
  138.             }
  139.         }
  140.         //获取数据库中0表0行0列的值
  141.         public static string GetFirst(DataSet ds)
  142.         {
  143.             if (isNull(ds))
  144.             {
  145.                 return null;
  146.             }
  147.             else
  148.             {
  149.                 return ds.Tables[0].Rows[0][0] + "";
  150.             }
  151.         }
  152.     }
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|西兴社区 ( 蜀ICP备2022005627号 )|网站地图

GMT+8, 2024-9-17 04:29 , Processed in 0.612605 second(s), 21 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表