TypeConverter.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. using System;
  2. using System.Globalization;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. namespace CP.Common
  7. {
  8. /// <summary>
  9. /// 数据转换类
  10. /// </summary>
  11. public class TypeConverter
  12. {
  13. /// <summary>
  14. /// 短期数
  15. /// </summary>
  16. /// <param name="qi"></param>
  17. /// <returns></returns>
  18. public static string GetShortQi(string qi)
  19. {
  20. if (string.IsNullOrEmpty(qi))
  21. return "";
  22. if (qi.Trim().Length >= 7)
  23. {
  24. return qi.Substring(4, 3);
  25. }
  26. return "";
  27. }
  28. public static DateTime IntToDateTime(int d)
  29. {
  30. /*
  31. DateTime time = DateTime.MinValue;
  32. DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
  33. time = startTime.AddSeconds(d);
  34. return time;
  35. */
  36. DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
  37. long lTime = long.Parse(d + "0000000");
  38. TimeSpan toNow = new TimeSpan(lTime);
  39. return dtStart.Add(toNow);
  40. }
  41. /// <summary>
  42. /// datetime转化为长整形
  43. /// </summary>
  44. /// <param name="time"></param>
  45. /// <returns></returns>
  46. public static int DateTimeToInt(DateTime time)
  47. {
  48. /*
  49. DateTime timeStamp = new DateTime(1970, 1, 1);
  50. int a = ObjectToInt((time.Ticks - timeStamp.Ticks) / 10000000);
  51. return a;
  52. */
  53. if (time == null)
  54. return 0;
  55. System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
  56. return (int)((DateTime)time - startTime).TotalSeconds;
  57. }
  58. /// <summary>
  59. /// 日期格式化成Long
  60. /// </summary>
  61. /// <param name="time"></param>
  62. /// <returns></returns>
  63. public static long DateTimeToLong(DateTime time)
  64. {
  65. DateTime timeStamp = new DateTime(1970, 1, 1);
  66. long a = (DateTime.UtcNow.Ticks - timeStamp.Ticks) / 10000000;
  67. return a;
  68. }
  69. public static string SubString(string str, int len)
  70. {
  71. if (str.Length > len)
  72. {
  73. return str.Substring(0, len);
  74. }
  75. return str;
  76. }
  77. /// <summary>
  78. /// 获取单项奖金
  79. /// </summary>
  80. /// <param name="num"></param>
  81. /// <returns></returns>
  82. public static string GetItemStr(string num)
  83. {
  84. if (string.IsNullOrWhiteSpace(num)) return "--";
  85. var number = StrToFloat(num.Trim(), -1);
  86. if (number == -1) return "--";
  87. if (number == 0) return "0";
  88. var culture = (CultureInfo)CultureInfo.GetCultureInfo("zh-cn").Clone();
  89. culture.NumberFormat.NumberGroupSizes =
  90. culture.NumberFormat.CurrencyGroupSizes = new int[] { 3 };
  91. culture.NumberFormat.NumberGroupSeparator =
  92. culture.NumberFormat.CurrencyGroupSeparator = ",";
  93. return number.ToString("N0", culture);
  94. }
  95. /// <summary>
  96. /// index页1等奖金额获取
  97. /// </summary>
  98. /// <param name="money"></param>
  99. /// <returns></returns>
  100. public static string GetJo1Money(string money)
  101. {
  102. if (string.IsNullOrEmpty(money))
  103. return "";
  104. var number = StrToFloat(money, -1);
  105. if (number == -1) return "--";
  106. if (number == 0) return "";
  107. var culture = (CultureInfo)CultureInfo.GetCultureInfo("zh-cn").Clone();
  108. culture.NumberFormat.NumberGroupSizes =
  109. culture.NumberFormat.CurrencyGroupSizes = new int[] { 4 };
  110. culture.NumberFormat.NumberGroupSeparator =
  111. culture.NumberFormat.CurrencyGroupSeparator = ",";
  112. var array = number.ToString("N0", culture).ToString().Split(',');
  113. int count = array.Length;
  114. string result = string.Empty;
  115. //text-20 text-blue
  116. //亿以上
  117. if (count == 3)
  118. {
  119. //10亿以上
  120. if (array[0].Length > 1)
  121. {
  122. result = $"{(array[0])}.{SubString(array[1], 1)}亿";
  123. }
  124. else
  125. {
  126. result = $"{array[0]}.{SubString(array[1], 2)}亿";
  127. }
  128. }
  129. //亿以下
  130. if (count == 2)
  131. {
  132. result = $"{StrToInt(array[0])}万";
  133. }
  134. if (count == 1)
  135. {
  136. result = $"{StrToInt(array[0])}";
  137. }
  138. return result;
  139. }
  140. /// <summary>
  141. /// 获取金额
  142. /// </summary>
  143. /// <param name="money"></param>
  144. /// <param name="style"></param>
  145. /// <returns></returns>
  146. public static string GetNoFormatMoney(string money)
  147. {
  148. if (string.IsNullOrEmpty(money))
  149. return "";
  150. var number = StrToFloat(money, -1);
  151. if (number == -1) return "--";
  152. if (number == 0) return "0";
  153. var culture = (CultureInfo)CultureInfo.GetCultureInfo("zh-cn").Clone();
  154. culture.NumberFormat.NumberGroupSizes =
  155. culture.NumberFormat.CurrencyGroupSizes = new int[] { 4 };
  156. culture.NumberFormat.NumberGroupSeparator =
  157. culture.NumberFormat.CurrencyGroupSeparator = ",";
  158. var array = number.ToString("N0", culture).ToString().Split(',');
  159. int count = array.Length;
  160. string result = string.Empty;
  161. //text-20 text-blue
  162. //亿以上
  163. if (count == 3)
  164. {
  165. //10亿以上
  166. if (array[0].Length > 1)
  167. {
  168. result = $"{(array[0])}.{SubString(array[1], 1)}亿";
  169. }
  170. else
  171. {
  172. result = $"{array[0]}.{SubString(array[1], 2)}亿";
  173. }
  174. }
  175. //亿以下
  176. if (count == 2)
  177. {
  178. result = $"{StrToInt(array[0])}万";
  179. }
  180. if (count == 1)
  181. {
  182. result = $"{StrToInt(array[0])}";
  183. }
  184. return result;
  185. }
  186. /// <summary>
  187. /// 获取金额
  188. /// </summary>
  189. /// <param name="money"></param>
  190. /// <param name="style"></param>
  191. /// <returns></returns>
  192. public static string GetMoney(string money, string style = "")
  193. {
  194. var number = StrToFloat(money, -1);
  195. if (number == -1) return "--";
  196. if (number == 0) return "0";
  197. var culture = (CultureInfo)CultureInfo.GetCultureInfo("zh-cn").Clone();
  198. culture.NumberFormat.NumberGroupSizes =
  199. culture.NumberFormat.CurrencyGroupSizes = new int[] { 4 };
  200. culture.NumberFormat.NumberGroupSeparator =
  201. culture.NumberFormat.CurrencyGroupSeparator = ",";
  202. var array = number.ToString("N0", culture).ToString().Split(',');
  203. int count = array.Length;
  204. string result = string.Empty;
  205. //text-20 text-blue
  206. //亿以上
  207. if (count == 3)
  208. {
  209. //10亿以上
  210. if (array[0].Length > 1)
  211. {
  212. result = $"<b class=\"{style}\"><span class=\"text-20 {style}\">{(array[0])}</span>.{SubString(array[1], 1)}</b> 亿元";
  213. }
  214. else
  215. {
  216. result = $"<b class=\"{style}\"><span class=\"text-20 {style}\">{array[0]}</span>.{SubString(array[1], 2)}</b> 亿元";
  217. }
  218. }
  219. //亿以下
  220. if (count == 2)
  221. {
  222. result = $"<b class=\"{style}\">{StrToInt(array[0])}</b> 万元";
  223. }
  224. if (count == 1)
  225. {
  226. result = $"<b class=\"{style}\">{StrToInt(array[0])}</b> 元";
  227. }
  228. return result;
  229. }
  230. public static string GetJcStr(string num)
  231. {
  232. //<b class="text-blue"><%=info.xlStr %> <span class="text-20 text-blue">3</span>.677</b> 亿元
  233. var number = StrToFloat(num, -1);
  234. if (number == -1) return "--";
  235. if (number == 0.00) return "0";
  236. var culture = (CultureInfo)CultureInfo.GetCultureInfo("zh-cn").Clone();
  237. culture.NumberFormat.NumberGroupSizes =
  238. culture.NumberFormat.CurrencyGroupSizes = new int[] { 4 };
  239. culture.NumberFormat.NumberGroupSeparator =
  240. culture.NumberFormat.CurrencyGroupSeparator = ",";
  241. var array = number.ToString("N0", culture).ToString().Split(',');
  242. // return number.ToString("N0", culture).ToString();
  243. string result = string.Empty;
  244. switch (array.Count())
  245. {
  246. case 3:
  247. result = $"<span class=\"text-20 text-red\">{StrToInt(array[0])}</span>亿{StrToInt(array[1])}万元";
  248. break;
  249. case 2:
  250. result = $"{StrToInt(array[0])}万元";
  251. break;
  252. case 1:
  253. result = $"{StrToInt(array[0])}元";
  254. break;
  255. default:
  256. return "--";
  257. }
  258. return result;
  259. }
  260. /// <summary>
  261. /// string型转换为bool型
  262. /// </summary>
  263. /// <param name="strValue">要转换的字符串</param>
  264. /// <param name="defValue">缺省值</param>
  265. /// <returns>转换后的bool类型结果</returns>
  266. public static bool StrToBool(object expression, bool defValue)
  267. {
  268. if (expression != null)
  269. return StrToBool(expression, defValue);
  270. return defValue;
  271. }
  272. /// <summary>
  273. /// string型转换为bool型
  274. /// </summary>
  275. /// <param name="strValue">要转换的字符串</param>
  276. /// <param name="defValue">缺省值</param>
  277. /// <returns>转换后的bool类型结果</returns>
  278. public static bool StrToBool(string expression, bool defValue)
  279. {
  280. if (expression != null)
  281. {
  282. if (string.Compare(expression, "true", true) == 0)
  283. return true;
  284. else if (string.Compare(expression, "false", true) == 0)
  285. return false;
  286. }
  287. return defValue;
  288. }
  289. /// <summary>
  290. /// 将对象转换为Int32类型
  291. /// </summary>
  292. /// <param name="strValue">要转换的字符串</param>
  293. /// <param name="defValue">缺省值</param>
  294. /// <returns>转换后的int类型结果</returns>
  295. public static int ObjectToInt(object expression)
  296. {
  297. return ObjectToInt(expression, 0);
  298. }
  299. /// <summary>
  300. /// 将对象转换为Int32类型
  301. /// </summary>
  302. /// <param name="strValue">要转换的字符串</param>
  303. /// <param name="defValue">缺省值</param>
  304. /// <returns>转换后的int类型结果</returns>
  305. public static int ObjectToInt(object expression, int defValue)
  306. {
  307. if (expression != null)
  308. return StrToInt(expression.ToString(), defValue);
  309. return defValue;
  310. }
  311. /// <summary>
  312. /// 将对象转换为Int32类型,转换失败返回0
  313. /// </summary>
  314. /// <param name="str">要转换的字符串</param>
  315. /// <returns>转换后的int类型结果</returns>
  316. public static int StrToInt(string str)
  317. {
  318. return StrToInt(str, 0);
  319. }
  320. /// <summary>
  321. /// 将对象转换为Int32类型
  322. /// </summary>
  323. /// <param name="str">要转换的字符串</param>
  324. /// <param name="defValue">缺省值</param>
  325. /// <returns>转换后的int类型结果</returns>
  326. public static int StrToInt(string str, int defValue)
  327. {
  328. if (string.IsNullOrEmpty(str) || str.Trim().Length >= 11 || !Regex.IsMatch(str.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
  329. return defValue;
  330. int rv;
  331. if (Int32.TryParse(str, out rv))
  332. return rv;
  333. return Convert.ToInt32(StrToFloat(str, defValue));
  334. }
  335. /// <summary>
  336. /// string型转换为float型
  337. /// </summary>
  338. /// <param name="strValue">要转换的字符串</param>
  339. /// <param name="defValue">缺省值</param>
  340. /// <returns>转换后的int类型结果</returns>
  341. public static float StrToFloat(object strValue, float defValue)
  342. {
  343. if ((strValue == null))
  344. return defValue;
  345. return StrToFloat(strValue.ToString(), defValue);
  346. }
  347. /// <summary>
  348. /// string型转换为float型
  349. /// </summary>
  350. /// <param name="strValue">要转换的字符串</param>
  351. /// <param name="defValue">缺省值</param>
  352. /// <returns>转换后的int类型结果</returns>
  353. public static float ObjectToFloat(object strValue, float defValue)
  354. {
  355. if ((strValue == null))
  356. return defValue;
  357. return StrToFloat(strValue.ToString(), defValue);
  358. }
  359. /// <summary>
  360. /// string型转换为float型
  361. /// </summary>
  362. /// <param name="strValue">要转换的字符串</param>
  363. /// <param name="defValue">缺省值</param>
  364. /// <returns>转换后的int类型结果</returns>
  365. public static float ObjectToFloat(object strValue)
  366. {
  367. return ObjectToFloat(strValue.ToString(), 0);
  368. }
  369. /// <summary>
  370. /// string型转换为float型
  371. /// </summary>
  372. /// <param name="strValue">要转换的字符串</param>
  373. /// <returns>转换后的int类型结果</returns>
  374. public static float StrToFloat(string strValue)
  375. {
  376. if ((strValue == null))
  377. return 0;
  378. return StrToFloat(strValue.ToString(), 0);
  379. }
  380. /// <summary>
  381. /// string型转换为float型
  382. /// </summary>
  383. /// <param name="strValue">要转换的字符串</param>
  384. /// <param name="defValue">缺省值</param>
  385. /// <returns>转换后的int类型结果</returns>
  386. public static float StrToFloat(string strValue, float defValue)
  387. {
  388. if ((strValue == null) || (strValue.Length > 10))
  389. return defValue;
  390. float intValue = defValue;
  391. if (strValue != null)
  392. {
  393. bool IsFloat = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
  394. if (IsFloat)
  395. float.TryParse(strValue, out intValue);
  396. }
  397. return intValue;
  398. }
  399. /// <summary>
  400. /// 将对象转换为日期时间类型
  401. /// </summary>
  402. /// <param name="str">要转换的字符串</param>
  403. /// <param name="defValue">缺省值</param>
  404. /// <returns>转换后的int类型结果</returns>
  405. public static DateTime StrToDateTime(string str, DateTime defValue)
  406. {
  407. if (!string.IsNullOrEmpty(str))
  408. {
  409. DateTime dateTime;
  410. if (DateTime.TryParse(str, out dateTime))
  411. return dateTime;
  412. }
  413. return defValue;
  414. }
  415. /// <summary>
  416. /// 将对象转换为日期时间类型
  417. /// </summary>
  418. /// <param name="str">要转换的字符串</param>
  419. /// <returns>转换后的int类型结果</returns>
  420. public static DateTime StrToDateTime(string str)
  421. {
  422. return StrToDateTime(str, DateTime.Now);
  423. }
  424. /// <summary>
  425. /// 将对象转换为日期时间类型
  426. /// </summary>
  427. /// <param name="obj">要转换的对象</param>
  428. /// <returns>转换后的int类型结果</returns>
  429. public static DateTime ObjectToDateTime(object obj)
  430. {
  431. return ObjectToDateTime(obj, DateTime.Now);
  432. }
  433. /// <summary>
  434. /// 将对象转换为日期时间类型
  435. /// </summary>
  436. /// <param name="obj">要转换的对象</param>
  437. /// <param name="defValue">缺省值</param>
  438. /// <returns>转换后的int类型结果</returns>
  439. public static DateTime ObjectToDateTime(object obj, DateTime defValue)
  440. {
  441. if (obj == null)
  442. return defValue;
  443. return StrToDateTime(obj.ToString(), defValue);
  444. }
  445. /// <summary>
  446. /// 字符串转成整型数组
  447. /// </summary>
  448. /// <param name="idList">要转换的字符串</param>
  449. /// <returns>转换后的int类型结果</returns>
  450. public static int[] StringToIntArray(string idList)
  451. {
  452. return StringToIntArray(idList, -1);
  453. }
  454. /// <summary>
  455. /// 字符串转成整型数组
  456. /// </summary>
  457. /// <param name="idList">要转换的字符串</param>
  458. /// <param name="defValue">缺省值</param>
  459. /// <returns>转换后的int类型结果</returns>
  460. public static int[] StringToIntArray(string idList, int defValue)
  461. {
  462. if (string.IsNullOrEmpty(idList))
  463. return null;
  464. string[] strArr = Utils.SplitString(idList, ",");
  465. int[] intArr = new int[strArr.Length];
  466. for (int i = 0; i < strArr.Length; i++)
  467. intArr[i] = StrToInt(strArr[i], defValue);
  468. return intArr;
  469. }
  470. }
  471. }