using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks; namespace Xima.FreamWork.Common.Web
{
public class TypeConverter
{ /// <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)
return StrToBool(expression, defValue); return defValue;
} /// <summary>
/// string型转换为bool型
/// </summary>
/// <param name="strValue">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的bool类型结果</returns>
public static bool StrToBool(string expression, bool defValue)
{
if (expression != null)
{
if (string.Compare(expression, "true", true) == || string.Compare(expression, "on", true) == )
return true;
else if (string.Compare(expression, "false", true) == || string.Compare(expression, "off", true) == )
return false;
}
return defValue;
} /// <summary>
/// obj转换成string型
/// </summary>
/// <param name="expression"></param>
/// <returns></returns>
public static string ObjToStr(object expression)
{
if (expression != null)
{
return Convert.ToString(expression);
}
return string.Empty;
} /// <summary>
/// 将对象转换为Int32类型
/// </summary>
/// <param name="strValue">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的int类型结果</returns>
public static int ObjectToInt(object expression)
{
return ObjectToInt(expression, );
} /// <summary>
/// 将对象转换为Int32类型
/// </summary>
/// <param name="strValue">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的int类型结果</returns>
public static int ObjectToInt(object expression, int defValue)
{
if (expression != null)
return StrToInt(expression.ToString(), defValue); return defValue;
} /// <summary>
/// 将对象转换为Int32类型,转换失败返回0
/// </summary>
/// <param name="str">要转换的字符串</param>
/// <returns>转换后的int类型结果</returns>
public static int StrToInt(string str)
{
return StrToInt(str, );
} /// <summary>
/// 将对象转换为Byte类型
/// </summary>
/// <param name="expression">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的int类型结果</returns>
public static Byte ObjectToByte(object expression, Byte defValue)
{
if (expression != null)
return StrToByte(expression.ToString(), defValue);
return defValue;
} /// <summary>
/// 将对象转换为Byte类型
/// </summary>
/// <param name="str">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的int类型结果</returns>
public static Byte StrToByte(string str, Byte defValue)
{
if (string.IsNullOrEmpty(str) || str.Trim().Length > || !Regex.IsMatch(str.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
return defValue;
Byte rv;
if (Byte.TryParse(str, out rv))
return rv;
return rv == ? defValue : Convert.ToByte(StrToFloat(str, defValue));
} /// <summary>
/// 将对象转换为Int32类型
/// </summary>
/// <param name="str">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的int类型结果</returns>
public static int StrToInt(string str, int defValue)
{
if (string.IsNullOrEmpty(str) || str.Trim().Length >= || !Regex.IsMatch(str.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
return defValue; int rv;
if (Int32.TryParse(str, out rv))
return rv; return Convert.ToInt32(StrToFloat(str, defValue));
} /// <summary>
/// 将对象转换为Int32类型
/// </summary>
/// <param name="str">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的int类型结果</returns>
public static Int64 StrToInt64(string str, int defValue)
{
if (string.IsNullOrEmpty(str) || str.Trim().Length >= || !Regex.IsMatch(str.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
return defValue; Int64 rv;
if (Int64.TryParse(str, out rv))
return rv;
return Convert.ToInt64(StrToFloat(str, defValue));
} /// <summary>
/// string型转换为float型
/// </summary>
/// <param name="strValue">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的int类型结果</returns>
public static float StrToFloat(object strValue, float defValue)
{
if ((strValue == null))
return defValue; return StrToFloat(strValue.ToString(), defValue);
} /// <summary>
/// string型转换为float型
/// </summary>
/// <param name="strValue">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的int类型结果</returns>
public static float ObjectToFloat(object strValue, float defValue)
{
if ((strValue == null))
return defValue; return StrToFloat(strValue.ToString(), defValue);
} /// <summary>
/// string型转换为float型
/// </summary>
/// <param name="strValue">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的int类型结果</returns>
public static float ObjectToFloat(object strValue)
{
return ObjectToFloat(strValue.ToString(), );
} /// <summary>
/// string型转换为float型
/// </summary>
/// <param name="strValue">要转换的字符串</param>
/// <returns>转换后的int类型结果</returns>
public static float StrToFloat(object strValue)
{
if ((strValue == null))
return ; return StrToFloat(strValue.ToString(), );
} /// <summary>
/// string型转换为float型
/// </summary>
/// <param name="strValue">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的int类型结果</returns>
public static float StrToFloat(string strValue, float defValue)
{
if ((strValue == null) || (strValue.Length > ))
return defValue; float intValue = defValue;
{
var IsFloat = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
if (IsFloat)
float.TryParse(strValue, out intValue);
}
return intValue;
} /// <summary>
/// string型转换为Decimal型
/// </summary>
/// <param name="strValue">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的Decimal类型结果</returns>
public static decimal StrToDecimal(string strValue, decimal defValue)
{
if ((strValue == null) || (strValue.Length > ))
return defValue; decimal intValue = defValue;
if (strValue != null)
{
bool IsFloat = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
if (IsFloat)
decimal.TryParse(strValue, out intValue);
}
return intValue;
}
}
}

Web--TypeConverter的更多相关文章

  1. Struts2中的一个类型转换示例

    1.写一个属性文件,里面写好需要转换的类型数据,xwork-conversion.properties,解释: xwork-conversion.properties表示对所有action中的指定数据 ...

  2. Java五大框架

    2017-6-13 Lifusen 此文章仅代表个人观点,如有问题提出请联系Q:570429601 1.Hibernate (开放源代码的对象关系映射框架) Hibernate是一个开放源代码的对象关 ...

  3. [Web API] Web API 2 深入系列(6) Model绑定(上)

    目录 解决什么问题 Model元数据解析 复杂类型 ValueProvider ValueProviderFactory 解决什么问题 Model: Action方法上的参数 Model绑定: 对Ac ...

  4. Asp.Net Web API 2第十六课——Parameter Binding in ASP.NET Web API(参数绑定)

    导航 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.html. 本文主要来讲解以下内容: ...

  5. ASP.NET Web API中的参数绑定总结

    ASP.NET Web API中的action参数类型可以分为简单类型和复杂类型. HttpResponseMessage Put(int id, Product item) id是int类型,是简单 ...

  6. Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service

    实现目标 http://localhost:9000/rs/roomservice 为入口, http://localhost:9000/rs/roomservice/room为房间列表, http: ...

  7. org.springframework.web.bind.ServletRequestDataBinde

    org.springframework.validation Class DataBinder java.lang.Object org.springframework.validation.Data ...

  8. 自定义TypeConverter把基础类型转换为复杂类型

    原文(http://tech.it168.com/d/2008-06-30/200806300953554_all.shtml) TypeConverter对于编写ASP.NET Server Con ...

  9. Parameter Binding in ASP.NET Web API(参数绑定)

    Parameter Binding in ASP.NET Web API(参数绑定) 导航 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnbl ...

  10. ASP.NET Web API编程——模型验证与绑定

    1.模型验证 使用特性约束模型属性 可以使用System.ComponentModel.DataAnnotations提供的特性来限制模型. 例如,Required特性表示字段值不能为空,Range特 ...

随机推荐

  1. 主库增加表空间导致DG同步失败

    由于主库表空间不足,同事给表空间增加数据文件,第二天收到反馈说备库未同步. 1.主.备查看归档序列号,发现主.备归档正常同步. SQL>archive log list 2.在主库端查询v$ar ...

  2. sqli-libs(32-37(宽字节注入)关)

    补充知识:宽字节注入 定义:GB2312.GBK.GB18030.BIG5.Shift_JIS等这些都是常说的宽字节,实际上只有两字节.宽字节带来的安全问题主要是吃ASCII字符(一字节)的现象,即将 ...

  3. C语言 多文件编程

    C语言 多文件编程 分文件编程 把函数声明放在头文件xxx.h中,在主函数中包含相应头文件 在头文件对应的xxx.c中实现xxx.h声明的函数 防止头文件重复包含 1.当一个项目比较大时,往往都是分文 ...

  4. Go函数高级

    1. 函数的数据类型 package main import "fmt" func main() { /* go语言的数据类型: 基本数据类型: int,float,bool,st ...

  5. Xcode 内存泄露检查出现:nil returned from a method that is expected to return a non-null value iOS 解决方案。

    在 使用 Xcode 检查内存泄露时(cmd+shift+B)运行,出现了一个警告:nil returned from a method that is expected to return a no ...

  6. Python实现将图片以二进制格式保存到MySQL数据库中,以及取出:

    创建数据库表格式: CREATE TABLE photo ( photo_no int(6) unsigned NOT NULL auto_increment, image MEDIUMBLOB, P ...

  7. Java内存模型(JMM)那些事

    本文是库存文章,去年年底学习了慕课网的并发编程课程,今年年初看完了<深入理解Java虚拟机>这本书,但是很多内容忘得差不多了,打算写写博客回忆一下那些忘在脑后的知识点. 温故而知新 更多J ...

  8. oracle 11g快捷版的安装与使用

    oracle 11g快捷版的安装与使用 [oracle 11g 下载地址(]https://www.oracle.com/technetwork/cn/database/enterprise-edit ...

  9. JenKins docker 集群

    //tag 桉树有时间来搞 **阿斯蒂 啊 阿斯蒂

  10. windows 域控用户记住最后一次登录用户名

    1,记住最后一次登录用户名 运行 gpedit.msc命令 禁用设置