AspNet 常有功能函数1.0
1.net 获取客户端ip方法(此方法不是很准确)
public static string GetIP()
{
string str;
if
(!string.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]))
{
str =
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
if (str.Split(new char[] { ‘.‘ },
StringSplitOptions.RemoveEmptyEntries).Length == 4)
{
return str;
}
}
if
(!string.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]))
{
str =
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
if (str.Split(new char[] { ‘.‘ },
StringSplitOptions.RemoveEmptyEntries).Length == 4)
{
}
} return str;
return
HttpContext.Current.Request.UserHostAddress;
}
2.获取浏览器语言
HttpContext.Current.Request.ServerVariables["HTTP_ACCEPT_LANGUAGE"].ToString();
3.日期字符串转换为标准日期
public static string GetDateTime(object values, string defaultvalues, string
strFormat)
{
if
(string.IsNullOrEmpty(Convert.ToString(values)))
{
return defaultvalues;
}
return
DateTime.ParseExact(values.ToString(), strFormat,
null).ToString("yyyy-MM-dd");
}
4.转换为decimal
public decimal GetDecimal(object obj, decimal defaultvalue)
{
decimal result = defaultvalue;
if (obj!=null
&& Regex.IsMatch(obj.ToString(), @"^([+-]?)\d*[.]?\d*$"))
{
result = Convert.ToDecimal(obj);
}
return result;
}
4.转换为double
public double GetDouble(object obj, double defaultvalue)
{
double result = defaultvalue;
if (obj != null &&
Regex.IsMatch(obj.ToString(), @"^([+-]?)\d*[.]?\d*$"))
{
result
=Math.Round(Convert.ToDouble(obj),2,MidpointRounding.ToEven);
}
return result;
}
5.转换为Int
public int GetInt(object obj, int defalutvaule)
{
int result = defalutvaule;
if (obj != null &&
Regex.IsMatch(obj.ToString(), @"^\d*$"))
{
result =Convert.ToInt32(obj);
}
return result;
}
6.获取request参数
public string GetParamValue(string paramkey,int type)
{
string result = "";
if (type==1)
{
result= HttpContext.Current.Request.QueryString[paramkey] != null ?
HttpContext.Current.Request.QueryString[paramkey].ToString() : "";
}
if(type==2){
result=
HttpContext.Current.Request.Form[paramkey] != null ?
HttpContext.Current.Request.Form[paramkey].ToString() : "";
}
return result;
}
7.金额转为大写
public static string CapitalNum(string numeric)
{
string str = numeric;
string str2 = "";
string
str3 = "仟佰拾亿仟佰拾万仟佰拾元角分";
str = str + "00";
int index =
str.IndexOf(".");
if (index >= 0)
{
str = str.Substring(0, index) + str.Substring(index + 1, 2);
}
str3 = str3.Substring(str3.Length - str.Length);
for (int i = 0; i < str.Length; i++)
{
str2 = str2 + "零壹贰叁肆伍陆柒捌玖".Substring(int.Parse(str.Substring(i, 1)), 1) +
str3.Substring(i, 1);
}
return str2;
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。