/// <summary> /// 字符串转换成double /// </summary> /// <param name="_str">字符串</param> /// <param name="_double">默认值</param> /// <returns></returns> public static double GetDouble(string _str, double _double) { if (!string.IsNullOrEmpty(_str)) { try { _double = FgFuncStr.NullToDouble(_str); } catch { } } return(_double); }
/// <summary> /// 字符串转换成decimal /// </summary> /// <param name="_str">字符串</param> /// <param name="_decimal">默认值</param> /// <returns></returns> public static decimal GetDecimal(string _str, decimal _decimal) { if (!string.IsNullOrEmpty(_str)) { try { _decimal = FgFuncStr.NullToDecimal(_str); } catch { } } return(_decimal); }
/// <summary> /// 转换为double,空对象转换为0.0 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static double NullToDouble(object obj) { if (obj == null || obj == System.DBNull.Value) //数据库空可以不判断,系统会自动转换为null { return(0.0); } else { return(FgFuncStr.ToDouble(obj.ToString())); } }
/// <summary> /// 字符串转换成Int值 /// </summary> /// <param name="_str">字符串</param> /// <param name="_defaut">int默认值</param> /// <param name="_max">最大值</param> /// <param name="_min">最小值</param> /// <returns></returns> public static int GetInt(string _str, int _defaut, int _max = 0, int _min = 0) { if (string.IsNullOrEmpty(_str)) { return(_defaut); } int _temp = 0; try { _temp = FgFuncStr.NullToInt(_str); } catch { return(_defaut); } if ((_temp >= _min && _temp <= _max) || (_max == _min && _max == 0)) { return(_temp); } return(_defaut); }
/// <summary> /// 根据出生日期算出年龄 /// </summary> /// <param name="Brithday">出生日期</param> /// <param name="DisArea">是否中文格式显示</param> /// <returns></returns> public static string GetAge(string Brithday, bool DisArea = false) { string _sRtAge = string.Empty;// 年龄的字符串表示 DateTime _Brithday = DateTime.Now; DateTime _DtNow = DateTime.Now; int _iYear = 0; // 岁 int _iMonth = 0; // 月 int _iDay = 0; // 天 //转换成日期失败,返回原字符串 try { _Brithday = FgFuncStr.Obj2DateTime(Brithday); } catch { return(""); } //计算天数 _iDay = _DtNow.Day - _Brithday.Day; if (_iDay < 0) { _DtNow = _DtNow.AddMonths(-1); _iDay += DateTime.DaysInMonth(_DtNow.Year, _DtNow.Month); } //计算月数 _iMonth = _DtNow.Month - _Brithday.Month; if (_iMonth < 0) { _iMonth += 12; _DtNow = _DtNow.AddYears(-1); } //计算年数 _iYear = _DtNow.Year - _Brithday.Year; //一岁以下可以输出天数 if (_iYear < 1) { if (_iMonth < 1)//小于1个月按天显示,如25天 { _sRtAge = _iDay.ToString() + "天"; } else if (_iMonth >= 1 && _iMonth < 3)//大于等于1个月小于3个月按月+天显示,如 1月12天 { _sRtAge = _iMonth.ToString() + "月"; if (_iDay != 0) { _sRtAge += _iDay.ToString() + "天"; } } else if (_iMonth >= 3 && _iMonth < 12)//大于等于3个月,小于12个月按月显示,如6月 { _sRtAge = _iMonth.ToString() + "月"; } } else { // if (_iYear >= 1 && _iYear < 14)//大于等于1岁小于14岁,按年+月显示,如3岁5月 // { // _sRtAge = _iYear.ToString() + "岁"; // if (_iMonth != 0) _sRtAge += _iMonth.ToString() + "月"; // } // else if (_iYear >= 14)//大于等于14岁,按年显示,如16岁 // _sRtAge = _iYear.ToString() + "岁"; _sRtAge = _iYear.ToString() + "岁" + _iMonth.ToString() + "月" + _iDay.ToString() + "天"; } if (DisArea) { _sRtAge = _sRtAge.Replace('岁', 'Y').Replace('月', 'M').Replace('天', 'D'); } return(_sRtAge); }