| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- using System;
- using System.Collections.Generic;
- using System.Text.RegularExpressions;
- using System.Threading;
- namespace Muchinfo.PC.Common.Helpers
- {
- /// <summary>
- /// 格式化帮助类
- /// </summary>
- public static class FormatHelper
- {
- #region Fields
- /// <summary>
- /// 日期格式 yyyy/MM/dd
- /// </summary>
- public const string DateFormat = "yyyy/MM/dd";
- /// <summary>
- /// 日期格式 yyyyMMdd
- /// </summary>
- public const string DateFormat2 = "yyyyMMdd";
- /// <summary>
- /// 日期格式 MM/dd
- /// </summary>
- public const string DateFormat3 = "MM/dd";
- /// <summary>
- /// 日期时间格式 yyyy-MM-dd HH:mm:ss
- /// </summary>
- public const string DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
- /// <summary>
- /// 日期时间格式 yyyy-MM-dd HH:mm:ss.fff
- /// </summary>
- public const string DateTimeFormat2 = "yyyy-MM-dd HH:mm:ss.fff";
- /// <summary>
- /// 日期时间格式 yyyyMMddHHmmss
- /// </summary>
- public const string DateTimeFormat3 = "yyyyMMddHHmmss";
- /// <summary>
- /// 一级分隔符 ','
- /// </summary>
- public const char Level1SplitChar = ',';
- /// <summary>
- /// 二级分隔符 '_'
- /// </summary>
- public const char Level2SplitChar = '_';
- /// <summary>
- /// 三级分隔符 ':'
- /// </summary>
- public const char Level3SplitChar = ':';
- /// <summary>
- /// 数字格式 0.? N位小数
- /// </summary>
- public const string NumberDecimalFormat = "f{0}";
- /// <summary>
- /// 数字格式 0.00 两位小数
- /// </summary>
- public const string NumberFormat = "f2";
- /// <summary>
- /// 数字格式 整数
- /// </summary>
- public const string NumberFormat2 = "f0";
- /// <summary>
- /// 数字科学计数法格式化
- /// </summary>
- public const string NumberNotationFormat = "0.000E+00";
- /// <summary>
- /// 数字百分比格式 ?%
- /// </summary>
- public const string PercentFormat = "p2";
- /// <summary>
- /// 数字百分比格式化带0两位小数
- /// </summary>
- public const string PercentFormat2 = "#0.00";
- /// <summary>
- /// 数字百分比格式 0.000%
- /// </summary>
- public const string PercentFormat3 = "p3";
- /// <summary>
- /// 时间格式 HH:mm:ss
- /// </summary>
- public const string TimeFormat = "HH:mm:ss";
- /// <summary>
- /// 时间格式 HHmm
- /// </summary>
- public const string TimeFormat2 = "HHmm";
- /// <summary>
- /// 时间格式 HH:mm
- /// </summary>
- public const string TimeFormat3 = "HH:mm";
- private static OrdinalIgnoreCaseStringEqualityComparer stringEqualityComparer = new OrdinalIgnoreCaseStringEqualityComparer();
- #endregion Fields
- #region Properties
- #region Public Static Properties
- /// <summary>
- ///获取字符串比较器
- /// </summary>
- public static OrdinalIgnoreCaseStringEqualityComparer StringEqualityComparer
- {
- get { return stringEqualityComparer; }
- }
- #endregion Public Static Properties
- #endregion Properties
- #region Methods
- #region Public Static Methods
- /// <summary>
- /// 从无格式化紧凑日期时间字符串还原回日期时间类型
- /// </summary>
- /// <param name="dateTime">日期时间</param>
- /// <returns>日期时间实例</returns>
- 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;
- }
- /// <summary>
- /// 转换成中文金额
- /// </summary>
- /// <param name="x">The x.</param>
- /// <returns>System.String.</returns>
- 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());
- }
- /// <summary>
- /// 转换成中文金额
- /// </summary>
- /// <param name="x">The x.</param>
- /// <returns>System.String.</returns>
- 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());
- }
- /// <summary>
- /// 从无格式化紧凑时间字符串还原回日期时间类型
- /// </summary>
- /// <param name="time">时间</param>
- /// <param name="useNow">是否使用系统当前时间</param>
- /// <returns>日期时间实例</returns>
- public static DateTime DateTimeFromTimeString(string time, bool useNow)
- {
- DateTime value = DateTime.MinValue;
- if (useNow) value = DateTime.Now.Date;
- return FromTimeString(value, time);
- }
- /// <summary>
- /// 从无格式化紧凑时间字符串还原回日期时间类型
- /// </summary>
- /// <param name="time">时间</param>
- /// <param name="dateTime">日期</param>
- /// <returns>日期时间实例</returns>
- public static DateTime DateTimeFromTimeString(string time, DateTime dateTime)
- {
- DateTime value = dateTime;
- return FromTimeString(value, time);
- }
- /// <summary>
- /// 将百分比数值转换为字符串
- /// </summary>
- /// <param name="rate">百分比数值</param>
- /// <param name="percentPlus">是否显示百分比符号</param>
- /// <returns>字符串</returns>
- 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
- /// <summary>
- /// Froms the time string.
- /// </summary>
- /// <param name="value">The value.</param>
- /// <param name="time">The time.</param>
- /// <returns>DateTime.</returns>
- 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
- }
- /// <summary>
- /// 字符串大小写比较器
- /// </summary>
- public class OrdinalIgnoreCaseStringEqualityComparer : IEqualityComparer<string>
- {
- #region Methods
- #region Public Methods
- /// <summary>
- /// 序号排序规则忽略大小写对比两个字符串是否相等
- /// </summary>
- /// <param name="x">字符串1</param>
- /// <param name="y">字符串2</param>
- /// <returns>是否相等</returns>
- public bool Equals(string x, string y)
- {
- return string.Equals(x, y, StringComparison.OrdinalIgnoreCase);
- }
- /// <summary>
- /// 获取字符串对象的哈希码
- /// </summary>
- /// <param name="obj">字符串对象</param>
- /// <returns>哈希码</returns>
- public int GetHashCode(string obj)
- {
- return obj.ToUpperInvariant().GetHashCode();
- }
- #endregion Public Methods
- #endregion Methods
- }
- }
|