namespace System { /// /// 格式化、类型转换 单元 /// public partial class FormatCom { private const string Dateformat = "yyyy-MM-dd"; #region ToDecimal /// /// 转化为 十进制表示 /// /// 字符串 /// public static Decimal ToDecimal(string inStr) { Decimal Result = 0; if (inStr != null) { if (Decimal.TryParse(inStr, out Result) == false) return 0; } return Result; } /// /// 转化为 十进制表示 /// /// 要转换的对象 /// public static Decimal ToDecimal(object obj) { Decimal Result = 0; if (null != obj) { if (Decimal.TryParse(obj.ToString(), out Result) == false) return 0; } return Result; } #endregion ToDecimal #region ToInt类型 /// /// 字符串转化为 整型数字表示 /// /// 需要转换的字符串 /// public static int ToInt(string inStr) { int Result = 0; if (inStr != null) { int pos = inStr.IndexOf('.'); if (pos > 0) { inStr = inStr.Substring(0, pos); } if (int.TryParse(inStr, out Result) == false) return 0; } return Result; } /// /// Decimal 转化为 整型数字表示 /// /// 需要转换的十进制数字 /// public static int ToInt(Decimal inDec) { return ToInt(inDec.ToString()); } /// /// Decimal 转化为 整型数字表示 /// /// 需要转换的十进制数字 /// public static int ToInt(double dounumber) { return ToInt(dounumber.ToString()); } /// /// object 转化为 整型数字表示 /// /// 需要转换的对象 /// public static int ToInt(object obj) { if (obj == null) return 0; return ToInt(obj.ToString()); } #endregion ToInt类型 #region ToInt32类型 /// /// 字符串转化为 整型数字表示 /// /// 需要转换的字符串 /// public static int ToInt32(string inStr) { int Result = 0; if (inStr != null) { int pos = inStr.IndexOf('.'); if (pos > 0) { inStr = inStr.Substring(0, pos); } if (int.TryParse(inStr, out Result) == false) return 0; } return Result; } /// /// Decimal 转化为 整型数字表示 /// /// 需要转换的十进制数字 /// public static int ToInt32(Decimal inDec) { return ToInt(inDec.ToString()); } /// /// Decimal 转化为 整型数字表示 /// /// 需要转换的十进制数字 /// public static int ToInt32(double dounumber) { return ToInt(dounumber.ToString()); } /// /// object 转化为 整型数字表示 /// /// 需要转换的对象 /// public static int ToInt32(object obj) { if (obj == null) return 0; return ToInt(obj.ToString()); } #endregion ToInt32类型 #region ToLong类型 /// /// 字符串转化为 long整型数字表示 /// /// 需要转换的字符串 /// public static long ToLong(string inStr) { long Result = 0; if (inStr != null) { if (long.TryParse(inStr, out Result) == false) return 0; } return Result; } /// /// Decimal 转化为 long整型数字表示 /// /// 需要转换的十进制数字 /// public static long ToLong(Decimal inDec) { long Result = 0; string inStr = inDec.ToString(); int pos = inStr.IndexOf('.'); inStr = inStr.Substring(0, pos); if (inStr != null) { if (long.TryParse(inStr, out Result) == false) return 0; } return Result; } /// /// object 转化为 long整型数字表示 /// /// 需要转换的对象 /// public static long ToLong(object obj) { long Result = 0; if (null != obj) { if (long.TryParse(obj.ToString(), out Result) == false) return 0; } return Result; } #endregion ToLong类型 #region toShort类型 /// /// 字符串转化为 短整数表示 /// /// /// public static short ToShort(string inStr) { short Result = 0; if (inStr != null) { if (short.TryParse(inStr, out Result) == false) return 0; } return Result; } /// /// Decimal 转化为 短整数表示 /// /// /// public static short ToShort(Decimal inDec) { short Result = 0; string inStr = inDec.ToString(); int pos = inStr.IndexOf('.'); inStr = inStr.Substring(0, pos); if (inStr != null) { if (short.TryParse(inStr, out Result) == false) return 0; } return Result; } /// /// object 转化为 短整数表示 /// /// /// public static short ToShort(object obj) { short Result = 0; if (!obj.Equals(null)) { if (short.TryParse(obj.ToString(), out Result) == false) return 0; } return Result; } #endregion toShort类型 #region 各种类型toString() /// /// 过滤字符串 /// /// /// public static string String(string str) { if (str != null) { str = str.Replace("&", "&"); str = str.Replace(" ", " "); str = str.Replace("
", "\n"); str = str.Replace(">", ">"); str = str.Replace("<", "<"); str = str.Replace(""", "\""); str = str.Replace("''", "'"); str = str.Replace("'", "'"); } else str = ""; return str; } public static string ToString(string str) { return String(str); } #endregion 各种类型toString() #region Double /// /// 判断是否是 实数 /// /// /// public static bool IsReal(string strnum) { bool result = false; try { double curin = Convert.ToDouble(strnum); result = true; } catch { result = false; } return result; } /// /// string 转化为 双精度实数表示 /// /// /// public static double ToDouble(string str) { if (str == null || str == "") return 0; else { double db = 0; if (Double.TryParse(str, out db) == false) return 0; else return db; } } public static double ToDouble(object obj) { if (obj == null || obj.ToString() == "") return 0; else { double db = 0; if (Double.TryParse(obj.ToString(), out db) == false) return 0; else return db; } } #endregion Double #region 字符串格式化 /// /// 空字符串 /// /// /// /// [Obsolete("函数过期 建议使用 LeftSpace")] public static string LeftNull(int num) { string curStr = ""; for (int i = 0; i < num; i++) { curStr += @" "; } return curStr; } /// ///空字符串 /// /// 空字符串 数量 /// public static string LeftSpace(int spaceNum) { string curStr = ""; for (int i = 0; i < spaceNum; i++) { curStr += @" "; } return curStr; } /// /// 左边加空格 /// /// /// /// [Obsolete("函数过期 建议使用 LeftSpaceStr")] public static string LeftNullStr(string str, int num) { string curStr = ""; int Len = str.Length; if (Len > num) { return str.Substring(Len - num, num); } else { curStr = LeftSpace(num - Len) + str; return curStr; } } /// /// 左边空字符串 /// /// /// /// public static string LeftSpaceStr(string str, int spaceNum) { int Len = str.Length; if (Len > spaceNum) { return str.Substring(Len - spaceNum, spaceNum); } else { return LeftSpace(spaceNum - Len) + str; } } /// /// 左边空字符串 /// /// /// /// public static string LeftSpaceStr(int inNumber, int spaceNum) { string str = inNumber.ToString(); return LeftSpaceStr(str, spaceNum); } /// /// 左边添加0 /// /// /// public static string Leftzero(int num) { string curStr = ""; for (int i = 0; i < num; i++) { curStr += "0"; } return curStr; } /// /// 左边添加0 /// /// /// /// public static string LeftzeroStr(string str, int formatLen) { int len = str.Length; if (len >= formatLen) { return str.Substring(len - formatLen, formatLen); } else { return Leftzero(formatLen - len) + str; } } /// /// 格式化 固定位数字符 不足用0补齐 /// /// /// /// public static string LeftzeroStr(int inNum, int formatLen) { string str = inNum.ToString(); return LeftzeroStr(str, formatLen); } #endregion 字符串格式化 #region 格式化标准函数 private const int NumNumber = 0; private const int priceNumber = 2; private const int MoneyNumber = 2; #region Number public static string Number(object numStr) { return KeepDigit(FormatCom.ToDecimal(numStr), NumNumber); } /// /// 双精度字符转换为整数形式字符串显示 /// /// 需要操作的数字 /// public static string Number(double numStr) { return KeepDigit((decimal)numStr, NumNumber); } /// /// 双精度字符转换为整数形式字符串显示 /// /// 需要操作的数字 /// public static string Number(System.Nullable numStr) { decimal curNum = 0; if (numStr != null) { curNum = (decimal)numStr; } return KeepDigit(curNum, NumNumber); } /// /// 十进制字符转换为整数形式字符串显示 /// /// 需要操作的数字 /// public static string Number(decimal numStr) { return KeepDigit(numStr, NumNumber); } /// /// 数字字符串转换为整数形式字符串显示 /// /// 需要操作的数字 /// public static string Number(string numStr) { return KeepDigit(numStr, NumNumber); } #endregion Number #region price 价格格式化 public static string Price(object numStr) { return KeepDigit(FormatCom.ToDecimal(numStr), priceNumber); } /// /// 双精度字符转换为单价形式字符串显示 /// /// 需要进行格式化的数字 /// public static string Price(double numStr) { return KeepDigit((decimal)numStr, priceNumber); } /// /// 双精度字符转换为单价形式字符串显示 /// /// 需要进行格式化的数字 /// public static string Price(System.Nullable numStr) { decimal curNum = 0; if (numStr != null) { curNum = (decimal)numStr; } return KeepDigit(curNum, priceNumber); } /// /// 十进制字符转换为单价形式字符串显示 /// /// 需要进行格式化的数字 /// public static string Price(decimal numStr) { return KeepDigit(numStr, priceNumber); } /// /// 数字字符串转换为单价形式字符串显示 /// /// 需要进行格式化的数字字符串 /// public static string Price(string numStr) { return KeepDigit(numStr, priceNumber); } #endregion price 价格格式化 #region money 金额 public static string Money(object numStr) { return KeepDigit(FormatCom.ToDecimal(numStr), MoneyNumber); } /// /// 双精度字符转换为金额形式字符串显示 /// /// 需要进行格式化的数字 /// public static string Money(double numStr) { return KeepDigit((decimal)numStr, MoneyNumber); } /// /// 十进制字符转换为金额形式字符串显示 /// /// 需要进行格式化的数字 /// public static string Money(System.Nullable numStr) { return KeepDigit((decimal)numStr, MoneyNumber); } /// /// 十进制字符转换为金额形式字符串显示 /// /// 需要进行格式化的数字 /// public static string Money(decimal numStr) { return KeepDigit(numStr, MoneyNumber); } /// /// 数字字符转换为金额形式字符串显示 /// /// 需要进行格式化的数字字符串 /// public static string Money(string numStr) { return KeepDigit(numStr, MoneyNumber); } #endregion money 金额 #region 格式化函数 /// /// 保留小数位数(用于String) /// /// 字符串 /// 保留位数 public static string KeepDigit(string numStr, int count) { string s = numStr; string Formatstr = "0."; decimal d = 0; decimal.TryParse(s.Trim(), out d); for (int i = 0; i < count; i++) { Formatstr += "0"; } return d.ToString(Formatstr); } /// /// 保留小数位数(用于decimal) /// /// 十进制数字 /// 保留位数 public static string KeepDigit(decimal inDecdecimal, int count) { string Formatstr = "0."; for (int i = 0; i < count; i++) { Formatstr += "0"; } return inDecdecimal.ToString(Formatstr); } /// /// /// /// /// /// public static string KeepDigit(double indouble, int count) { string Formatstr = "0."; for (int i = 0; i < count; i++) { Formatstr += "0"; } return indouble.ToString(Formatstr); } #endregion 格式化函数 #endregion 格式化标准函数 } }