123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514 |
- using System;
- using System.Globalization;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace CP.Common
- {
- /// <summary>
- /// 数据转换类
- /// </summary>
- public class TypeConverter
- {
- /// <summary>
- /// 短期数
- /// </summary>
- /// <param name="qi"></param>
- /// <returns></returns>
- public static string GetShortQi(string qi)
- {
- if (string.IsNullOrEmpty(qi))
- return "";
- if (qi.Trim().Length >= 7)
- {
- return qi.Substring(4, 3);
- }
- return "";
- }
- public static DateTime IntToDateTime(int d)
- {
- /*
- DateTime time = DateTime.MinValue;
- DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
- time = startTime.AddSeconds(d);
- return time;
- */
- DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
- long lTime = long.Parse(d + "0000000");
- TimeSpan toNow = new TimeSpan(lTime);
- return dtStart.Add(toNow);
- }
- /// <summary>
- /// datetime转化为长整形
- /// </summary>
- /// <param name="time"></param>
- /// <returns></returns>
- public static int DateTimeToInt(DateTime time)
- {
- /*
- DateTime timeStamp = new DateTime(1970, 1, 1);
- int a = ObjectToInt((time.Ticks - timeStamp.Ticks) / 10000000);
- return a;
- */
- if (time == null)
- return 0;
- System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
- return (int)((DateTime)time - startTime).TotalSeconds;
- }
- /// <summary>
- /// 日期格式化成Long
- /// </summary>
- /// <param name="time"></param>
- /// <returns></returns>
- public static long DateTimeToLong(DateTime time)
- {
- DateTime timeStamp = new DateTime(1970, 1, 1);
- long a = (DateTime.UtcNow.Ticks - timeStamp.Ticks) / 10000000;
- return a;
- }
- public static string SubString(string str, int len)
- {
- if (str.Length > len)
- {
- return str.Substring(0, len);
- }
- return str;
- }
- /// <summary>
- /// 获取单项奖金
- /// </summary>
- /// <param name="num"></param>
- /// <returns></returns>
- public static string GetItemStr(string num)
- {
- if (string.IsNullOrWhiteSpace(num)) return "--";
- var number = StrToFloat(num.Trim(), -1);
- if (number == -1) return "--";
- if (number == 0) return "0";
- var culture = (CultureInfo)CultureInfo.GetCultureInfo("zh-cn").Clone();
- culture.NumberFormat.NumberGroupSizes =
- culture.NumberFormat.CurrencyGroupSizes = new int[] { 3 };
- culture.NumberFormat.NumberGroupSeparator =
- culture.NumberFormat.CurrencyGroupSeparator = ",";
- return number.ToString("N0", culture);
- }
- /// <summary>
- /// index页1等奖金额获取
- /// </summary>
- /// <param name="money"></param>
- /// <returns></returns>
- public static string GetJo1Money(string money)
- {
- if (string.IsNullOrEmpty(money))
- return "";
- var number = StrToFloat(money, -1);
- if (number == -1) return "--";
- if (number == 0) return "";
- var culture = (CultureInfo)CultureInfo.GetCultureInfo("zh-cn").Clone();
- culture.NumberFormat.NumberGroupSizes =
- culture.NumberFormat.CurrencyGroupSizes = new int[] { 4 };
- culture.NumberFormat.NumberGroupSeparator =
- culture.NumberFormat.CurrencyGroupSeparator = ",";
- var array = number.ToString("N0", culture).ToString().Split(',');
- int count = array.Length;
- string result = string.Empty;
- //text-20 text-blue
- //亿以上
- if (count == 3)
- {
- //10亿以上
- if (array[0].Length > 1)
- {
- result = $"{(array[0])}.{SubString(array[1], 1)}亿";
- }
- else
- {
- result = $"{array[0]}.{SubString(array[1], 2)}亿";
- }
- }
- //亿以下
- if (count == 2)
- {
- result = $"{StrToInt(array[0])}万";
- }
- if (count == 1)
- {
- result = $"{StrToInt(array[0])}";
- }
- return result;
- }
- /// <summary>
- /// 获取金额
- /// </summary>
- /// <param name="money"></param>
- /// <param name="style"></param>
- /// <returns></returns>
- public static string GetNoFormatMoney(string money)
- {
- if (string.IsNullOrEmpty(money))
- return "";
- var number = StrToFloat(money, -1);
- if (number == -1) return "--";
- if (number == 0) return "0";
- var culture = (CultureInfo)CultureInfo.GetCultureInfo("zh-cn").Clone();
- culture.NumberFormat.NumberGroupSizes =
- culture.NumberFormat.CurrencyGroupSizes = new int[] { 4 };
- culture.NumberFormat.NumberGroupSeparator =
- culture.NumberFormat.CurrencyGroupSeparator = ",";
- var array = number.ToString("N0", culture).ToString().Split(',');
- int count = array.Length;
- string result = string.Empty;
- //text-20 text-blue
- //亿以上
- if (count == 3)
- {
- //10亿以上
- if (array[0].Length > 1)
- {
- result = $"{(array[0])}.{SubString(array[1], 1)}亿";
- }
- else
- {
- result = $"{array[0]}.{SubString(array[1], 2)}亿";
- }
- }
- //亿以下
- if (count == 2)
- {
- result = $"{StrToInt(array[0])}万";
- }
- if (count == 1)
- {
- result = $"{StrToInt(array[0])}";
- }
- return result;
- }
- /// <summary>
- /// 获取金额
- /// </summary>
- /// <param name="money"></param>
- /// <param name="style"></param>
- /// <returns></returns>
- public static string GetMoney(string money, string style = "")
- {
- var number = StrToFloat(money, -1);
- if (number == -1) return "--";
- if (number == 0) return "0";
- var culture = (CultureInfo)CultureInfo.GetCultureInfo("zh-cn").Clone();
- culture.NumberFormat.NumberGroupSizes =
- culture.NumberFormat.CurrencyGroupSizes = new int[] { 4 };
- culture.NumberFormat.NumberGroupSeparator =
- culture.NumberFormat.CurrencyGroupSeparator = ",";
- var array = number.ToString("N0", culture).ToString().Split(',');
- int count = array.Length;
- string result = string.Empty;
- //text-20 text-blue
- //亿以上
- if (count == 3)
- {
- //10亿以上
- if (array[0].Length > 1)
- {
- result = $"<b class=\"{style}\"><span class=\"text-20 {style}\">{(array[0])}</span>.{SubString(array[1], 1)}</b> 亿元";
- }
- else
- {
- result = $"<b class=\"{style}\"><span class=\"text-20 {style}\">{array[0]}</span>.{SubString(array[1], 2)}</b> 亿元";
- }
- }
- //亿以下
- if (count == 2)
- {
- result = $"<b class=\"{style}\">{StrToInt(array[0])}</b> 万元";
- }
- if (count == 1)
- {
- result = $"<b class=\"{style}\">{StrToInt(array[0])}</b> 元";
- }
- return result;
- }
- public static string GetJcStr(string num)
- {
- //<b class="text-blue"><%=info.xlStr %> <span class="text-20 text-blue">3</span>.677</b> 亿元
- var number = StrToFloat(num, -1);
- if (number == -1) return "--";
- if (number == 0.00) return "0";
- var culture = (CultureInfo)CultureInfo.GetCultureInfo("zh-cn").Clone();
- culture.NumberFormat.NumberGroupSizes =
- culture.NumberFormat.CurrencyGroupSizes = new int[] { 4 };
- culture.NumberFormat.NumberGroupSeparator =
- culture.NumberFormat.CurrencyGroupSeparator = ",";
- var array = number.ToString("N0", culture).ToString().Split(',');
- // return number.ToString("N0", culture).ToString();
- string result = string.Empty;
- switch (array.Count())
- {
- case 3:
- result = $"<span class=\"text-20 text-red\">{StrToInt(array[0])}</span>亿{StrToInt(array[1])}万元";
- break;
- case 2:
- result = $"{StrToInt(array[0])}万元";
- break;
- case 1:
- result = $"{StrToInt(array[0])}元";
- break;
- default:
- return "--";
- }
- return result;
- }
- /// <summary>
- /// string型转换为bool型
- /// </summary>
- /// <param name="strValue">要转换的字符串</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的bool类型结果</returns>
- public static bool StrToBool(object expression, bool defValue)
- {
- if (expression != null)
- return StrToBool(expression, defValue);
- return defValue;
- }
- /// <summary>
- /// string型转换为bool型
- /// </summary>
- /// <param name="strValue">要转换的字符串</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的bool类型结果</returns>
- public static bool StrToBool(string expression, bool defValue)
- {
- if (expression != null)
- {
- if (string.Compare(expression, "true", true) == 0)
- return true;
- else if (string.Compare(expression, "false", true) == 0)
- return false;
- }
- return defValue;
- }
- /// <summary>
- /// 将对象转换为Int32类型
- /// </summary>
- /// <param name="strValue">要转换的字符串</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的int类型结果</returns>
- public static int ObjectToInt(object expression)
- {
- return ObjectToInt(expression, 0);
- }
- /// <summary>
- /// 将对象转换为Int32类型
- /// </summary>
- /// <param name="strValue">要转换的字符串</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的int类型结果</returns>
- public static int ObjectToInt(object expression, int defValue)
- {
- if (expression != null)
- return StrToInt(expression.ToString(), defValue);
- return defValue;
- }
- /// <summary>
- /// 将对象转换为Int32类型,转换失败返回0
- /// </summary>
- /// <param name="str">要转换的字符串</param>
- /// <returns>转换后的int类型结果</returns>
- public static int StrToInt(string str)
- {
- return StrToInt(str, 0);
- }
- /// <summary>
- /// 将对象转换为Int32类型
- /// </summary>
- /// <param name="str">要转换的字符串</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的int类型结果</returns>
- public static int StrToInt(string str, int defValue)
- {
- if (string.IsNullOrEmpty(str) || str.Trim().Length >= 11 || !Regex.IsMatch(str.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
- return defValue;
- int rv;
- if (Int32.TryParse(str, out rv))
- return rv;
- return Convert.ToInt32(StrToFloat(str, defValue));
- }
- /// <summary>
- /// string型转换为float型
- /// </summary>
- /// <param name="strValue">要转换的字符串</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的int类型结果</returns>
- public static float StrToFloat(object strValue, float defValue)
- {
- if ((strValue == null))
- return defValue;
- return StrToFloat(strValue.ToString(), defValue);
- }
- /// <summary>
- /// string型转换为float型
- /// </summary>
- /// <param name="strValue">要转换的字符串</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的int类型结果</returns>
- public static float ObjectToFloat(object strValue, float defValue)
- {
- if ((strValue == null))
- return defValue;
- return StrToFloat(strValue.ToString(), defValue);
- }
- /// <summary>
- /// string型转换为float型
- /// </summary>
- /// <param name="strValue">要转换的字符串</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的int类型结果</returns>
- public static float ObjectToFloat(object strValue)
- {
- return ObjectToFloat(strValue.ToString(), 0);
- }
- /// <summary>
- /// string型转换为float型
- /// </summary>
- /// <param name="strValue">要转换的字符串</param>
- /// <returns>转换后的int类型结果</returns>
- public static float StrToFloat(string strValue)
- {
- if ((strValue == null))
- return 0;
- return StrToFloat(strValue.ToString(), 0);
- }
- /// <summary>
- /// string型转换为float型
- /// </summary>
- /// <param name="strValue">要转换的字符串</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的int类型结果</returns>
- public static float StrToFloat(string strValue, float defValue)
- {
- if ((strValue == null) || (strValue.Length > 10))
- return defValue;
- float intValue = defValue;
- if (strValue != null)
- {
- bool IsFloat = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
- if (IsFloat)
- float.TryParse(strValue, out intValue);
- }
- return intValue;
- }
- /// <summary>
- /// 将对象转换为日期时间类型
- /// </summary>
- /// <param name="str">要转换的字符串</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的int类型结果</returns>
- public static DateTime StrToDateTime(string str, DateTime defValue)
- {
- if (!string.IsNullOrEmpty(str))
- {
- DateTime dateTime;
- if (DateTime.TryParse(str, out dateTime))
- return dateTime;
- }
- return defValue;
- }
- /// <summary>
- /// 将对象转换为日期时间类型
- /// </summary>
- /// <param name="str">要转换的字符串</param>
- /// <returns>转换后的int类型结果</returns>
- public static DateTime StrToDateTime(string str)
- {
- return StrToDateTime(str, DateTime.Now);
- }
- /// <summary>
- /// 将对象转换为日期时间类型
- /// </summary>
- /// <param name="obj">要转换的对象</param>
- /// <returns>转换后的int类型结果</returns>
- public static DateTime ObjectToDateTime(object obj)
- {
- return ObjectToDateTime(obj, DateTime.Now);
- }
- /// <summary>
- /// 将对象转换为日期时间类型
- /// </summary>
- /// <param name="obj">要转换的对象</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的int类型结果</returns>
- public static DateTime ObjectToDateTime(object obj, DateTime defValue)
- {
- if (obj == null)
- return defValue;
- return StrToDateTime(obj.ToString(), defValue);
- }
- /// <summary>
- /// 字符串转成整型数组
- /// </summary>
- /// <param name="idList">要转换的字符串</param>
- /// <returns>转换后的int类型结果</returns>
- public static int[] StringToIntArray(string idList)
- {
- return StringToIntArray(idList, -1);
- }
- /// <summary>
- /// 字符串转成整型数组
- /// </summary>
- /// <param name="idList">要转换的字符串</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的int类型结果</returns>
- public static int[] StringToIntArray(string idList, int defValue)
- {
- if (string.IsNullOrEmpty(idList))
- return null;
- string[] strArr = Utils.SplitString(idList, ",");
- int[] intArr = new int[strArr.Length];
- for (int i = 0; i < strArr.Length; i++)
- intArr[i] = StrToInt(strArr[i], defValue);
- return intArr;
- }
- }
- }
|