asp.net数据类型转换类实例

 代码如下 复制代码

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace TypeClass

{
    public class TypeParse
    {
        /// <summary>
        /// 判断对象是否为Int32类型的数字
        /// </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型转换为bool型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的bool类型结果</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>
        /// 将对象转换为Int32类型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的Int32类型结果</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型转换为float型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的float类型结果</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型转换为Decimal型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的Decimal类型结果</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型转换为datetime型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的datetime类型结果</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>
        /// 判断给定的字符串数组(strNumber)中的数据是不是都为数值型
        /// </summary>
        /// <param name="strNumber">要确认的字符串数组</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;

        }
    }
}

时间: 2024-09-27 16:48:10

asp.net数据类型转换类实例的相关文章

asp.net 数据类型转换类代码_实用技巧

复制代码 代码如下: using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; namespace TypeClass { public class TypeParse { /// <summary> /// 判断对象是否为Int32类型的数字 /// </summary> /// <param name="Expr

ASP.NET缓存处理类实例

 本文实例讲述了ASP.NET缓存处理类.分享给大家供大家参考.具体如下: ASP.NET 缓存处理类. 用法: Just copy this code into a new class file (.cs) and add it to your ASP .NET website. One thing to keep in mind is that data stored in ASP .NET Cache can be accessible across all sessions. So wh

php实现的返回数据格式化类实例_php技巧

本文实例讲述了php实现的返回数据格式化类及其用法,在字符串处理中非常具有实用价值.分享给大家供大家参考.具体方法如下: DataReturn.class.php类文件如下: <?php /** 返回数据格式化类 * Date: 2011-08-15 * Author: fdipzone */ class DataReturn{ // class start private $type; private $xmlroot; private $callback; private $returnDa

php实现的返回数据格式化类实例

 DataReturn.class.php类文件如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

ASP.NET 数据访问类

asp.net|访问|数据 using System;using System.Data;using System.Data.SqlClient; namespace SysClassLibrary{ /// <summary> /// DataAccess 的摘要说明. /// <description>数据处理基类,调用方式:DataAccess.DataSet((string)sqlstr);或者DataAccess.DataSet((string)sqlstr,ref Da

Asp.Net 数据操作类(附通用数据基类)_实用技巧

using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; namespace EC {

ASP.NET 数据访问类_实用技巧

using System; using System.Data; using System.Data.SqlClient;  namespace SysClassLibrary { /// <summary> /// DataAccess 的摘要说明. /// <description>数据处理基类,调用方式:DataAccess.DataSet((string)sqlstr);或者DataAccess.DataSet((string)sqlstr,ref DataSet ds);

asp 大数据量分页实例代码

<!doctype html public "-//w3c//dtd html 4.0 transitional//en"> <html><head><title>后台</title> <meta http-equiv=content-type content="text/html; charset=gb2312"> <% set rs=server.createobject("

asp的通用数据分页类_ASP CLASS类

 (原创)<!--#include file="Conn.asp" --> 通用数据分页类     通用分页类,以后写分页显示数据时就轻松多啦.直接调用此类,然后再Execute即可以取得当前页的所有数据.     此类所做的工作是只取得当前页的数据,和总页数和总记录数等等数据. ASP代码: <% '/*****************************分页显示类************************** '/* 作者:哇哇鱼 '/* 日期:2004