FormatHelper.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. using System.Threading;
  5. namespace Muchinfo.PC.Common.Helpers
  6. {
  7. /// <summary>
  8. /// 格式化帮助类
  9. /// </summary>
  10. public static class FormatHelper
  11. {
  12. #region Fields
  13. /// <summary>
  14. /// 日期格式 yyyy/MM/dd
  15. /// </summary>
  16. public const string DateFormat = "yyyy/MM/dd";
  17. /// <summary>
  18. /// 日期格式 yyyyMMdd
  19. /// </summary>
  20. public const string DateFormat2 = "yyyyMMdd";
  21. /// <summary>
  22. /// 日期格式 MM/dd
  23. /// </summary>
  24. public const string DateFormat3 = "MM/dd";
  25. /// <summary>
  26. /// 日期时间格式 yyyy-MM-dd HH:mm:ss
  27. /// </summary>
  28. public const string DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
  29. /// <summary>
  30. /// 日期时间格式 yyyy-MM-dd HH:mm:ss.fff
  31. /// </summary>
  32. public const string DateTimeFormat2 = "yyyy-MM-dd HH:mm:ss.fff";
  33. /// <summary>
  34. /// 日期时间格式 yyyyMMddHHmmss
  35. /// </summary>
  36. public const string DateTimeFormat3 = "yyyyMMddHHmmss";
  37. /// <summary>
  38. /// 一级分隔符 ','
  39. /// </summary>
  40. public const char Level1SplitChar = ',';
  41. /// <summary>
  42. /// 二级分隔符 '_'
  43. /// </summary>
  44. public const char Level2SplitChar = '_';
  45. /// <summary>
  46. /// 三级分隔符 ':'
  47. /// </summary>
  48. public const char Level3SplitChar = ':';
  49. /// <summary>
  50. /// 数字格式 0.? N位小数
  51. /// </summary>
  52. public const string NumberDecimalFormat = "f{0}";
  53. /// <summary>
  54. /// 数字格式 0.00 两位小数
  55. /// </summary>
  56. public const string NumberFormat = "f2";
  57. /// <summary>
  58. /// 数字格式 整数
  59. /// </summary>
  60. public const string NumberFormat2 = "f0";
  61. /// <summary>
  62. /// 数字科学计数法格式化
  63. /// </summary>
  64. public const string NumberNotationFormat = "0.000E+00";
  65. /// <summary>
  66. /// 数字百分比格式 ?%
  67. /// </summary>
  68. public const string PercentFormat = "p2";
  69. /// <summary>
  70. /// 数字百分比格式化带0两位小数
  71. /// </summary>
  72. public const string PercentFormat2 = "#0.00";
  73. /// <summary>
  74. /// 数字百分比格式 0.000%
  75. /// </summary>
  76. public const string PercentFormat3 = "p3";
  77. /// <summary>
  78. /// 时间格式 HH:mm:ss
  79. /// </summary>
  80. public const string TimeFormat = "HH:mm:ss";
  81. /// <summary>
  82. /// 时间格式 HHmm
  83. /// </summary>
  84. public const string TimeFormat2 = "HHmm";
  85. /// <summary>
  86. /// 时间格式 HH:mm
  87. /// </summary>
  88. public const string TimeFormat3 = "HH:mm";
  89. private static OrdinalIgnoreCaseStringEqualityComparer stringEqualityComparer = new OrdinalIgnoreCaseStringEqualityComparer();
  90. #endregion Fields
  91. #region Properties
  92. #region Public Static Properties
  93. /// <summary>
  94. ///获取字符串比较器
  95. /// </summary>
  96. public static OrdinalIgnoreCaseStringEqualityComparer StringEqualityComparer
  97. {
  98. get { return stringEqualityComparer; }
  99. }
  100. #endregion Public Static Properties
  101. #endregion Properties
  102. #region Methods
  103. #region Public Static Methods
  104. /// <summary>
  105. /// 从无格式化紧凑日期时间字符串还原回日期时间类型
  106. /// </summary>
  107. /// <param name="dateTime">日期时间</param>
  108. /// <returns>日期时间实例</returns>
  109. public static DateTime DateTimeFromString(string dateTime)
  110. {
  111. DateTime value = DateTime.MinValue;
  112. int year = value.Year, month = value.Month, day = value.Day, hour = 0, minute = 0, seconds = 0;
  113. if (dateTime.Length >= 4)
  114. {
  115. if (!int.TryParse(dateTime.Substring(0, 4), out year))
  116. year = value.Year;
  117. }
  118. if (dateTime.Length >= 6)
  119. {
  120. if (!int.TryParse(dateTime.Substring(4, 2), out month))
  121. month = value.Month;
  122. }
  123. if (dateTime.Length >= 8)
  124. {
  125. if (!int.TryParse(dateTime.Substring(6, 2), out day))
  126. day = value.Day;
  127. }
  128. if (dateTime.Length >= 10)
  129. {
  130. if (!int.TryParse(dateTime.Substring(8, 2), out hour))
  131. hour = 0;
  132. }
  133. if (dateTime.Length >= 12)
  134. {
  135. if (!int.TryParse(dateTime.Substring(10, 2), out minute))
  136. minute = 0;
  137. }
  138. if (dateTime.Length >= 14)
  139. {
  140. if (!int.TryParse(dateTime.Substring(12, 2), out seconds))
  141. seconds = 0;
  142. }
  143. try
  144. {
  145. value = new DateTime(year, month, day, hour, minute, seconds);
  146. }
  147. catch (Exception e)
  148. {
  149. return DateTime.MinValue;
  150. //throw new MuchinfoException(ExceptionManager.UnknownClientErrorCode, e);
  151. }
  152. return value;
  153. }
  154. /// <summary>
  155. /// 转换成中文金额
  156. /// </summary>
  157. /// <param name="x">The x.</param>
  158. /// <returns>System.String.</returns>
  159. public static string ConvertToChinese(double x)
  160. {
  161. if (x.Equals(0)) return "零";
  162. string s = x.ToString("#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A");
  163. string d = Regex.Replace(s, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\.]|$))))", "${b}${z}");
  164. return Regex.Replace(d, ".", m => "负元空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟万亿兆"[m.Value[0] - '-'].ToString());
  165. }
  166. /// <summary>
  167. /// 转换成中文金额
  168. /// </summary>
  169. /// <param name="x">The x.</param>
  170. /// <returns>System.String.</returns>
  171. public static string ConvertToChinese(decimal x)
  172. {
  173. if (x.Equals(0) || x < 0) return "零";
  174. string s = x.ToString("#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A");
  175. string d = Regex.Replace(s, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\.]|$))))", "${b}${z}");
  176. return Regex.Replace(d, ".", m => "负元空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟万亿兆"[m.Value[0] - '-'].ToString());
  177. }
  178. /// <summary>
  179. /// 从无格式化紧凑时间字符串还原回日期时间类型
  180. /// </summary>
  181. /// <param name="time">时间</param>
  182. /// <param name="useNow">是否使用系统当前时间</param>
  183. /// <returns>日期时间实例</returns>
  184. public static DateTime DateTimeFromTimeString(string time, bool useNow)
  185. {
  186. DateTime value = DateTime.MinValue;
  187. if (useNow) value = DateTime.Now.Date;
  188. return FromTimeString(value, time);
  189. }
  190. /// <summary>
  191. /// 从无格式化紧凑时间字符串还原回日期时间类型
  192. /// </summary>
  193. /// <param name="time">时间</param>
  194. /// <param name="dateTime">日期</param>
  195. /// <returns>日期时间实例</returns>
  196. public static DateTime DateTimeFromTimeString(string time, DateTime dateTime)
  197. {
  198. DateTime value = dateTime;
  199. return FromTimeString(value, time);
  200. }
  201. /// <summary>
  202. /// 将百分比数值转换为字符串
  203. /// </summary>
  204. /// <param name="rate">百分比数值</param>
  205. /// <param name="percentPlus">是否显示百分比符号</param>
  206. /// <returns>字符串</returns>
  207. public static string RateToString(decimal rate, bool percentPlus)
  208. {
  209. if (percentPlus)
  210. return (rate / 100).ToString(PercentFormat, Thread.CurrentThread.CurrentCulture);
  211. else
  212. return rate.ToString(PercentFormat2, Thread.CurrentThread.CurrentCulture);
  213. }
  214. #endregion Public Static Methods
  215. #region Private Static Methods
  216. /// <summary>
  217. /// Froms the time string.
  218. /// </summary>
  219. /// <param name="value">The value.</param>
  220. /// <param name="time">The time.</param>
  221. /// <returns>DateTime.</returns>
  222. private static DateTime FromTimeString(DateTime value, string time)
  223. {
  224. int hour = 0, minute = 0, seconds = 0;
  225. if (time.Length >= 2)
  226. {
  227. if (!int.TryParse(time.Substring(0, 2), out hour))
  228. hour = 0;
  229. }
  230. if (time.Length >= 4)
  231. {
  232. if (!int.TryParse(time.Substring(2, 2), out minute))
  233. minute = 0;
  234. }
  235. if (time.Length >= 6)
  236. {
  237. if (!int.TryParse(time.Substring(4, 2), out seconds))
  238. seconds = 0;
  239. }
  240. try
  241. {
  242. return new DateTime(value.Year, value.Month, value.Day, hour, minute, seconds);
  243. }
  244. catch
  245. {
  246. return value;
  247. }
  248. }
  249. #endregion Private Static Methods
  250. #endregion Methods
  251. }
  252. /// <summary>
  253. /// 字符串大小写比较器
  254. /// </summary>
  255. public class OrdinalIgnoreCaseStringEqualityComparer : IEqualityComparer<string>
  256. {
  257. #region Methods
  258. #region Public Methods
  259. /// <summary>
  260. /// 序号排序规则忽略大小写对比两个字符串是否相等
  261. /// </summary>
  262. /// <param name="x">字符串1</param>
  263. /// <param name="y">字符串2</param>
  264. /// <returns>是否相等</returns>
  265. public bool Equals(string x, string y)
  266. {
  267. return string.Equals(x, y, StringComparison.OrdinalIgnoreCase);
  268. }
  269. /// <summary>
  270. /// 获取字符串对象的哈希码
  271. /// </summary>
  272. /// <param name="obj">字符串对象</param>
  273. /// <returns>哈希码</returns>
  274. public int GetHashCode(string obj)
  275. {
  276. return obj.ToUpperInvariant().GetHashCode();
  277. }
  278. #endregion Public Methods
  279. #endregion Methods
  280. }
  281. }