using System; using System.Collections.Generic; using System.Text.RegularExpressions; using System.Threading; namespace Muchinfo.PC.Common.Helpers { /// /// 格式化帮助类 /// public static class FormatHelper { #region Fields /// /// 日期格式 yyyy/MM/dd /// public const string DateFormat = "yyyy/MM/dd"; /// /// 日期格式 yyyyMMdd /// public const string DateFormat2 = "yyyyMMdd"; /// /// 日期格式 MM/dd /// public const string DateFormat3 = "MM/dd"; /// /// 日期时间格式 yyyy-MM-dd HH:mm:ss /// public const string DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; /// /// 日期时间格式 yyyy-MM-dd HH:mm:ss.fff /// public const string DateTimeFormat2 = "yyyy-MM-dd HH:mm:ss.fff"; /// /// 日期时间格式 yyyyMMddHHmmss /// public const string DateTimeFormat3 = "yyyyMMddHHmmss"; /// /// 一级分隔符 ',' /// public const char Level1SplitChar = ','; /// /// 二级分隔符 '_' /// public const char Level2SplitChar = '_'; /// /// 三级分隔符 ':' /// public const char Level3SplitChar = ':'; /// /// 数字格式 0.? N位小数 /// public const string NumberDecimalFormat = "f{0}"; /// /// 数字格式 0.00 两位小数 /// public const string NumberFormat = "f2"; /// /// 数字格式 整数 /// public const string NumberFormat2 = "f0"; /// /// 数字科学计数法格式化 /// public const string NumberNotationFormat = "0.000E+00"; /// /// 数字百分比格式 ?% /// public const string PercentFormat = "p2"; /// /// 数字百分比格式化带0两位小数 /// public const string PercentFormat2 = "#0.00"; /// /// 数字百分比格式 0.000% /// public const string PercentFormat3 = "p3"; /// /// 时间格式 HH:mm:ss /// public const string TimeFormat = "HH:mm:ss"; /// /// 时间格式 HHmm /// public const string TimeFormat2 = "HHmm"; /// /// 时间格式 HH:mm /// public const string TimeFormat3 = "HH:mm"; private static OrdinalIgnoreCaseStringEqualityComparer stringEqualityComparer = new OrdinalIgnoreCaseStringEqualityComparer(); #endregion Fields #region Properties #region Public Static Properties /// ///获取字符串比较器 /// public static OrdinalIgnoreCaseStringEqualityComparer StringEqualityComparer { get { return stringEqualityComparer; } } #endregion Public Static Properties #endregion Properties #region Methods #region Public Static Methods /// /// 从无格式化紧凑日期时间字符串还原回日期时间类型 /// /// 日期时间 /// 日期时间实例 public static DateTime DateTimeFromString(string dateTime) { DateTime value = DateTime.MinValue; int year = value.Year, month = value.Month, day = value.Day, hour = 0, minute = 0, seconds = 0; if (dateTime.Length >= 4) { if (!int.TryParse(dateTime.Substring(0, 4), out year)) year = value.Year; } if (dateTime.Length >= 6) { if (!int.TryParse(dateTime.Substring(4, 2), out month)) month = value.Month; } if (dateTime.Length >= 8) { if (!int.TryParse(dateTime.Substring(6, 2), out day)) day = value.Day; } if (dateTime.Length >= 10) { if (!int.TryParse(dateTime.Substring(8, 2), out hour)) hour = 0; } if (dateTime.Length >= 12) { if (!int.TryParse(dateTime.Substring(10, 2), out minute)) minute = 0; } if (dateTime.Length >= 14) { if (!int.TryParse(dateTime.Substring(12, 2), out seconds)) seconds = 0; } try { value = new DateTime(year, month, day, hour, minute, seconds); } catch (Exception e) { return DateTime.MinValue; //throw new MuchinfoException(ExceptionManager.UnknownClientErrorCode, e); } return value; } /// /// 转换成中文金额 /// /// The x. /// System.String. public static string ConvertToChinese(double x) { if (x.Equals(0)) return "零"; string s = x.ToString("#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A"); 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}"); return Regex.Replace(d, ".", m => "负元空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟万亿兆"[m.Value[0] - '-'].ToString()); } /// /// 转换成中文金额 /// /// The x. /// System.String. public static string ConvertToChinese(decimal x) { if (x.Equals(0) || x < 0) return "零"; string s = x.ToString("#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A"); 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}"); return Regex.Replace(d, ".", m => "负元空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟万亿兆"[m.Value[0] - '-'].ToString()); } /// /// 从无格式化紧凑时间字符串还原回日期时间类型 /// /// 时间 /// 是否使用系统当前时间 /// 日期时间实例 public static DateTime DateTimeFromTimeString(string time, bool useNow) { DateTime value = DateTime.MinValue; if (useNow) value = DateTime.Now.Date; return FromTimeString(value, time); } /// /// 从无格式化紧凑时间字符串还原回日期时间类型 /// /// 时间 /// 日期 /// 日期时间实例 public static DateTime DateTimeFromTimeString(string time, DateTime dateTime) { DateTime value = dateTime; return FromTimeString(value, time); } /// /// 将百分比数值转换为字符串 /// /// 百分比数值 /// 是否显示百分比符号 /// 字符串 public static string RateToString(decimal rate, bool percentPlus) { if (percentPlus) return (rate / 100).ToString(PercentFormat, Thread.CurrentThread.CurrentCulture); else return rate.ToString(PercentFormat2, Thread.CurrentThread.CurrentCulture); } #endregion Public Static Methods #region Private Static Methods /// /// Froms the time string. /// /// The value. /// The time. /// DateTime. private static DateTime FromTimeString(DateTime value, string time) { int hour = 0, minute = 0, seconds = 0; if (time.Length >= 2) { if (!int.TryParse(time.Substring(0, 2), out hour)) hour = 0; } if (time.Length >= 4) { if (!int.TryParse(time.Substring(2, 2), out minute)) minute = 0; } if (time.Length >= 6) { if (!int.TryParse(time.Substring(4, 2), out seconds)) seconds = 0; } try { return new DateTime(value.Year, value.Month, value.Day, hour, minute, seconds); } catch { return value; } } #endregion Private Static Methods #endregion Methods } /// /// 字符串大小写比较器 /// public class OrdinalIgnoreCaseStringEqualityComparer : IEqualityComparer { #region Methods #region Public Methods /// /// 序号排序规则忽略大小写对比两个字符串是否相等 /// /// 字符串1 /// 字符串2 /// 是否相等 public bool Equals(string x, string y) { return string.Equals(x, y, StringComparison.OrdinalIgnoreCase); } /// /// 获取字符串对象的哈希码 /// /// 字符串对象 /// 哈希码 public int GetHashCode(string obj) { return obj.ToUpperInvariant().GetHashCode(); } #endregion Public Methods #endregion Methods } }