ConverObject.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Core.StlMes.Client.Sale.Util
  6. {
  7. public class ConverObject
  8. {
  9. private static string _defaultDataTimeFormt = "yyyyMMddHHmmss";
  10. public static string ConvertDataTimeToString(DateTime value)
  11. {
  12. if(value == null || value.ToString().Length == 0)
  13. return "";
  14. return value.ToString(_defaultDataTimeFormt);
  15. }
  16. /// <summary>
  17. /// 将System.DateTime日期改为sql 认可的to_date
  18. /// </summary>
  19. /// <param name="value"></param>
  20. /// <returns></returns>
  21. public static string GetTimeStringValueToDateFunction(System.DateTime value)
  22. {
  23. return string.Format("to_date('{0}','yyyymmddhh24miss')", Util.ConverObject.ConvertDataTimeToString(value));
  24. }
  25. /// <summary>
  26. /// 将DateTime 类型转为sql 可以比较的类型
  27. /// </summary>
  28. /// <param name="dt"></param>
  29. /// <returns></returns>
  30. public static string ConvertDateTimeValueToDbLanguage(DateTime dt)
  31. {
  32. return string.Format(" trunc( to_date('{0}','yyyymmdd'))", dt.ToString("yyyyMMdd"));
  33. }
  34. /// <summary>
  35. /// 将DateTime 类型转为sql 可以比较的类型
  36. /// </summary>
  37. /// <param name="columnName"></param>
  38. /// <param name="beginDate"></param>
  39. /// <param name="endDate"></param>
  40. /// <returns></returns>
  41. public static string ConvertDateTimeValueToDbLanguage(string columnName, DateTime beginDate, DateTime endDate)
  42. {
  43. return string.Format(" and {0} >= trunc( to_date('{1}','yyyymmdd')) and {0} < trunc( to_date('{2}','yyyymmdd')) + 1 ",
  44. columnName, beginDate.ToString("yyyyMMdd"), endDate.ToString("yyyyMMdd"));
  45. }
  46. public static string ConverDateTimeToyyyyMMdd(DateTime time)
  47. {
  48. return time.ToString("yyyyMMdd");
  49. }
  50. public static string ConvertListStringValueToSqlCondition(string columnName,List<string> list)
  51. {
  52. if (list == null || list.Count == 0)
  53. return " and 1 = 2 ";
  54. System.Text.StringBuilder sb = new StringBuilder();
  55. foreach (String s in list)
  56. {
  57. sb.Append(string.Format(" {0} = '{1}' ",columnName,s)).Append(" or ");
  58. }
  59. string sqlCondition = " and (" + sb.ToString() + " 1 = 2 )";
  60. return sqlCondition;
  61. }
  62. public static string ConvertNumberToChinese(string strNum)
  63. {
  64. string[] Nums = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
  65. string[] Digits = { "", "拾", "佰", "仟" };
  66. string[] Units = { "元", "万", "亿", "万亿" };
  67. string x, y, z = "";
  68. if (strNum.Length > 2)
  69. {
  70. x = strNum.Substring(0, strNum.Length - 2);
  71. y = strNum.Substring(strNum.Length - 2, 2);
  72. }
  73. else
  74. {
  75. x = "";
  76. y = strNum;
  77. }
  78. if (y.Length == 2)
  79. {
  80. int n = Convert.ToInt32(y.Substring(0, 1));
  81. z = Nums[n] + "角";
  82. }
  83. if (y.Length > 0)
  84. {
  85. int n = Convert.ToInt32(y.Substring(y.Length - 1, 1));
  86. z += Nums[n] + "分";
  87. }
  88. if (y.Length == 0)
  89. {
  90. if (x.Length == 0)
  91. z = "零元整";
  92. else
  93. z = "整";
  94. }
  95. string S = ""; //返回值
  96. int p = 0; //字符位置指针
  97. int m = x.Length % 4; //取模
  98. // 四位一组得到组数
  99. int k = (m > 0 ? x.Length / 4 + 1 : x.Length / 4);
  100. // 外层循环在所有组中循环
  101. // 从左到右 高位到低位 四位一组 逐组处理
  102. // 每组最后加上一个单位: "[万亿]","[亿]","[万]"
  103. for (int i = k; i > 0; i--)
  104. {
  105. int L = 4;
  106. if (i == k && m != 0)
  107. {
  108. L = m;
  109. }
  110. // 得到一组四位数 最高位组有可能不足四位
  111. string s = x.Substring(p, L);
  112. int l = s.Length;
  113. // 内层循环在该组中的每一位数上循环 从左到右 高位到低位
  114. for (int j = 0; j < l; j++)
  115. {
  116. //处理改组中的每一位数加上所在位: "仟","佰","拾",""(个)
  117. int n = Convert.ToInt32(s.Substring(j, 1));
  118. if (n == 0)
  119. {
  120. if (j < l - 1
  121. && Convert.ToInt32(s.Substring(j + 1, 1)) > 0 //后一位(右低)
  122. && !S.EndsWith(Nums[n]))
  123. {
  124. S += Nums[n];
  125. }
  126. }
  127. else
  128. {
  129. //处理 1013 一千零"十三", 1113 一千一百"一十三"
  130. if (!(n == 1 && (S.EndsWith(Nums[0]) | S.Length == 0) && j == l - 2))
  131. {
  132. S += Nums[n];
  133. }
  134. S += Digits[l - j - 1];
  135. }
  136. }
  137. p += L;
  138. // 每组最后加上一个单位: [万],[亿] 等
  139. if (i < k) //不是最高位的一组
  140. {
  141. if (Convert.ToInt32(s) != 0)
  142. {
  143. //如果所有 4 位不全是 0 则加上单位 [万],[亿] 等
  144. S += Units[i - 1];
  145. }
  146. }
  147. else
  148. {
  149. //处理最高位的一组,最后必须加上单位
  150. S += Units[i - 1];
  151. }
  152. }
  153. return S + z;
  154. }
  155. public static double ConvertToDouble(object value)
  156. {
  157. try
  158. {
  159. return Convert.ToDouble(value);
  160. }
  161. catch
  162. {
  163. return 0;
  164. }
  165. }
  166. }
  167. }