教程集 www.jiaochengji.com
教程集 >  脚本编程  >  Asp.net  >  正文 asp.net数据类型转换类实例

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

发布时间:2018-09-05   编辑:jiaochengji.com
教程集为您提供asp.net数据类型转换类实例等资源,欢迎您收藏本站,我们将为您提供最新的asp.net数据类型转换类实例资源
本文章介绍了一个关于数据类型转换类文件,有需要的朋友可参考一下使用方法。
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1161')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1161>

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;

        }
    }
}

您可能感兴趣的文章:
C#泛型全面讲解
golang中的类型及类型转换指南
学习C#匿名类型与隐式类型变量
c#继承与多态示例解析
详解C#泛型及其特点
c# 类与成员的修饰详解
C#泛型:泛型特点、泛型继承、泛型接口、泛型委托学习笔记
Golang 类型转换、类型断言与类型选择
C#中的“装箱”(boxing)与“拆箱”(unboxing)
golang的类型转换(断言等)

[关闭]
~ ~