asp.net 數(shù)據(jù)類型轉(zhuǎn)換類代碼
更新時間:2012年06月07日 00:47:45 作者:
asp.net 數(shù)據(jù)類型轉(zhuǎn)換類代碼,需要的朋友可以參考下
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace TypeClass
{
public class TypeParse
{
/// <summary>
/// 判斷對象是否為Int32類型的數(shù)字
/// </summary>
/// <param name="Expression"></param>
/// <returns></returns>
public static bool IsNumeric(object Expression)
{
if (Expression != null)
{
int intVal;
return int.TryParse(Expression.ToString(), out intVal);
}
return false;
}
public static bool IsDouble(object Expression)
{
if (Expression != null)
{
double doubleVal;
return double.TryParse(Expression.ToString(), out doubleVal);
}
return false;
}
/// <summary>
/// string型轉(zhuǎn)換為bool型
/// </summary>
/// <param name="strValue">要轉(zhuǎn)換的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>轉(zhuǎn)換后的bool類型結(jié)果</returns>
public static bool StrToBool(object Expression, bool defValue)
{
if (Expression != null)
{
bool boolValue;
if (bool.TryParse(Expression.ToString(), out boolValue))
return boolValue;
else
return defValue;
}
return defValue;
}
/// <summary>
/// 將對象轉(zhuǎn)換為Int32類型
/// </summary>
/// <param name="strValue">要轉(zhuǎn)換的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>轉(zhuǎn)換后的Int32類型結(jié)果</returns>
public static int StrToInt(object Expression, int defValue)
{
if (Expression != null)
{
int intValue;
if (int.TryParse(Expression.ToString(), out intValue))
return intValue;
else
return defValue;
}
return defValue;
}
/// <summary>
/// string型轉(zhuǎn)換為float型
/// </summary>
/// <param name="strValue">要轉(zhuǎn)換的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>轉(zhuǎn)換后的float類型結(jié)果</returns>
public static float StrToFloat(object strValue, float defValue)
{
if (strValue != null)
{
float floatValue;
if (float.TryParse(strValue.ToString(), out floatValue))
return floatValue;
else
return defValue;
}
return defValue;
}
/// <summary>
/// string型轉(zhuǎn)換為Decimal型
/// </summary>
/// <param name="strValue">要轉(zhuǎn)換的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>轉(zhuǎn)換后的Decimal類型結(jié)果</returns>
public static Decimal StrToDecimal(object strValue, Decimal defValue)
{
if (strValue != null)
{
Decimal decimalValue;
if (Decimal.TryParse(strValue.ToString(), out decimalValue))
return Math.Round(decimalValue,2);
else
return defValue;
}
return defValue;
}
/// <summary>
/// string型轉(zhuǎn)換為datetime型
/// </summary>
/// <param name="strValue">要轉(zhuǎn)換的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>轉(zhuǎn)換后的datetime類型結(jié)果</returns>
public static DateTime StrToDateTime(object strValue, DateTime defValue)
{
if (strValue != null)
{
DateTime DateTimeValue;
if (DateTime.TryParse(strValue.ToString(), out DateTimeValue))
return DateTimeValue;
else
return defValue;
}
return defValue;
}
/// <summary>
/// 判斷給定的字符串?dāng)?shù)組(strNumber)中的數(shù)據(jù)是不是都為數(shù)值型
/// </summary>
/// <param name="strNumber">要確認(rèn)的字符串?dāng)?shù)組</param>
/// <returns>是則返加true 不是則返回 false</returns>
public static bool IsNumericArray(string[] strNumber)
{
if (strNumber == null)
{
return false;
}
if (strNumber.Length < 1)
{
return false;
}
foreach (string id in strNumber)
{
if (!IsNumeric(id))
{
return false;
}
}
return true;
}
}
}
相關(guān)文章
Asp.net TextBox的TextChanged事件使用介紹
動態(tài)創(chuàng)建的控件是如何加載視圖狀態(tài),還提到ProcessPostData方法的調(diào)用,這里我就用TextBox的TextChanged事件來說說視圖數(shù)據(jù)的加載以及事件的觸發(fā)2012-12-12
.NET的動態(tài)編譯與WS服務(wù)調(diào)用詳解
這篇文章介紹了.NET的動態(tài)編譯與WS服務(wù)調(diào)用詳解,有需要的朋友可以參考一下,希望對你有所幫助2013-07-07
.net數(shù)據(jù)庫操作框架SqlSugar的簡單入門
這篇文章主要介紹了.net數(shù)據(jù)庫操作框架SqlSugar的簡單入門,幫助大家更好的理解和學(xué)習(xí)使用.net技術(shù),感興趣的朋友可以了解下2021-04-04
ASP.NET MVC中使用log4net的實現(xiàn)示例
這篇文章主要介紹了ASP.NET MVC中使用log4net的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
Asp.net導(dǎo)出Excel/Csv文本格式數(shù)據(jù)的方法
這篇文章主要介紹了Asp.net導(dǎo)出Excel/Csv文本格式數(shù)據(jù)的方法,比較實用,需要的朋友可以參考下2014-09-09

