• 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;
    }

  • 相关阅读:
    Poj 2017 Speed Limit(水题)
    Poj 1316 Self Numbers(水题)
    Poj 1017 Packets(贪心策略)
    Poj 1017 Packets(贪心策略)
    Poj 2662,2909 Goldbach's Conjecture (素数判定)
    Poj 2662,2909 Goldbach's Conjecture (素数判定)
    poj 2388 Who's in the Middle(快速排序求中位数)
    poj 2388 Who's in the Middle(快速排序求中位数)
    poj 2000 Gold Coins(水题)
    poj 2000 Gold Coins(水题)
  • 原文地址:https://www.cnblogs.com/linsu/p/3522193.html
Copyright © 2020-2023  润新知