www.pudn.com > FerrariSystem.rar > CommonFunc.cs


using System; 
using System.Configuration; 
using System.Collections; 
using System.Text; 
 
namespace Common 
{ 
	///  
	/// CommonFunc 的摘要说明。 
	///  
	public class CommonFunc 
	{ 
		public CommonFunc() 
		{ 
			// 
			// TODO: 在此处添加构造函数逻辑 
			// 
		} 
 
		///  
		/// 取得数据库连接字符串 
		///  
		///  
		public static string getConnectionString() 
		{ 
			return ConfigurationSettings.AppSettings["SYS-CONN"]; 
		} 
 
		///  
		/// 取得消息页面显示的图片路径 
		///  
		///  
		public static string getMessagePic(string type) 
		{ 
			if (type=="1") 
			{ 
				return ConfigurationSettings.AppSettings["ErrorPicPath"];  
			} 
			else if (type=="2") 
			{ 
				return ConfigurationSettings.AppSettings["SuccessPicPath"]; 
			} 
			else 
			{ 
				return ""; 
			} 
		} 
 
		#region 返回页面显示用的数值 
		public static string ConvertNumericToPageStyle(Object objValue)  
		{ 
			return ConvertNumericToPageStyle(objValue,false); 
		} 
		 
		///  
		/// 返回页面显示用的数值 
		///  
		/// 对象(Int,Decimal,Float) 
		/// 是否允许负数 
		///  
		public static string ConvertNumericToPageStyle(Object objValue, bool isAllowNegative)  
		{ 
			if ( !isAllowNegative && decimal.Parse(objValue.ToString()) < 0) 
			{ 
				return ""; 
			} 
 
			string myValue = objValue.ToString(); 
			 
			//Integer 
			if (myValue.IndexOf(".") < 0) 
			{ 
				return myValue; 
			} 
			 
			//Decimal 
			bool isloop = false; 
			do  
			{ 
				//check the last bit 
				string temp = myValue.Substring(myValue.Length-1,1); 
				if (temp == "0")  
				{ 
					myValue = myValue.Substring(0,myValue.Length-1); 
				} 
				else if (myValue.Substring(myValue.Length-1,1) == ".") 
				{ 
					myValue = myValue.Substring(0,myValue.Length-1); 
					isloop = true; 
				} 
				else 
				{ 
					isloop = true; 
				} 
			} while (! isloop); 
 
			return myValue; 
		} 
		#endregion 
 
		#region 返回页面显示用的日期(yyyy年MM月dd日) 
		public static string ConvertDateToPageStyle(string strdate) 
		{ 
			try 
			{ 
				DateTime date = DateTime.Parse(strdate); 
				string year = date.Year.ToString(); 
				string month = date.Month.ToString(); 
				string day = date.Day.ToString(); 
 
				if (month.Length < 2) 
				{ 
					month = "0" + month; 
				} 
				if (day.Length < 2) 
				{ 
					day = "0" + day; 
				} 
				 
				string retDate = year + "年" + month + "月" + day + "日"; 
 
				return retDate; 
			} 
			catch 
			{ 
				return ""; 
			} 
		} 
	 
		#endregion 
 
		#region 返回页面显示用货币格式 
		///  
		/// 返回页面显示用货币格式 
		///  
		/// 对象(Int,Decimal,Float) 
		/// 货币标记 
		///  
		public static string ConvertMoneyToPageStyle(Object objValue,String flag) 
		{ 
			string strReturn = ""; 
			bool isNegative = false; 
			 
			//转换到页面显示用的数值(允许负数) 
			string strValue = ConvertNumericToPageStyle(objValue,true); 
 
			//判断有无小数 
			int index = strValue.IndexOf("."); 
			if (index >= 0) 
			{ 
				//保留小数部分 
				strReturn = strValue.Substring(index,strValue.Length - index); 
				//取得整数部分 
				strValue = strValue.Substring(0,index); 
			} 
 
			//判断是否负数 
			if (strValue.Substring(0,1) == "-") 
			{ 
				isNegative = true; 
				strValue = strValue.Substring(1,strValue.Length -1); 
			} 
			 
			//从后往前每三位增加一个分隔符 
			bool isLoop = false; 
			do 
			{ 
				if (strValue.Length > 3) 
				{ 
					strReturn = "," + strValue.Substring(strValue.Length -3) + strReturn; 
					strValue = strValue.Substring(0,strValue.Length -3); 
				}  
				else 
				{ 
					strReturn = strValue + strReturn; 
					isLoop = true; 
				} 
			} while (! isLoop); 
 
			//负号 
			if (isNegative)  
			{ 
				strReturn = "-" + strReturn; 
			} 
 
			//货币标记 
			strReturn = flag + strReturn; 
 
			return strReturn; 
		} 
 
		public static string ConvertMoneyToPageStyle(Object objValue) 
		{ 
			return ConvertMoneyToPageStyle(objValue,"¥"); 
		} 
		#endregion  
 
		 
 
 
	} 
}