栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > asp

asp.net 数据类型转换类代码

asp 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

asp.net 数据类型转换类代码

复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Regularexpressions;
namespace TypeClass
{
public class TypeParse
{
///
/// 判断对象是否为Int32类型的数字
///

///
///
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;
}
///
/// string型转换为bool型
///

/// 要转换的字符串
/// 缺省值
/// 转换后的bool类型结果
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;
}
///
/// 将对象转换为Int32类型
///

/// 要转换的字符串
/// 缺省值
/// 转换后的Int32类型结果
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;
}
///
/// string型转换为float型
///

/// 要转换的字符串
/// 缺省值
/// 转换后的float类型结果
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;
}
///
/// string型转换为Decimal型
///

/// 要转换的字符串
/// 缺省值
/// 转换后的Decimal类型结果
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;
}
///
/// string型转换为datetime型
///

/// 要转换的字符串
/// 缺省值
/// 转换后的datetime类型结果
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;
}
///
/// 判断给定的字符串数组(strNumber)中的数据是不是都为数值型
///

/// 要确认的字符串数组
/// 是则返加true 不是则返回 false
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;
}
}
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/58361.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号