常用的工具类2

public sealed class TypeHelper
{
#region Int16 / short
public static short ToInt16(object o, short _default)
{
try { return Convert.ToInt16(o); }
catch { }
short result = default(short);
if (!short.TryParse(o.ToString(), out result))
result = _default;
return result;
}

public static short ToInt16(object o)
{
return ToInt16(o, default(short));
}
#endregion

public static string ToStringNoPoint(string str)
{
//return str.TrimEnd(new char[] { ‘0‘, ‘.‘ });
return str.Replace(".00","").Replace(".0","");
}

#region Int32 / int
/// <summary>
/// 转换为int类型
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static int ToInt(object o, int _default)
{
try { return Convert.ToInt32(o); }
catch { }
int result = default(int);
if (!int.TryParse(o.ToString(), out result))
result = _default;
return result;
}

public static int ToInt(object o)
{
return ToInt(o, default(int));
}

public static int ToInt32(object o, int _default)
{
try { return Convert.ToInt32(o); }
catch { }
int result = default(int);
if (!int.TryParse(o.ToString(), out result))
result = _default;
return result;
}

public static int ToInt32(object o)
{
return ToInt32(o, default(int));
}
#endregion

#region Int64 / long
public static long ToInt64(object o, long _default)
{
try { return Convert.ToInt64(o); }
catch { }
long result = default(long);
if (!long.TryParse(o.ToString(), out result))
result = _default;
return result;
}

public static long ToInt64(object o)
{
return ToInt64(o, default(long));
}
#endregion

#region UInt16

public static UInt16 ToUInt16(object o, UInt16 _default)
{
try { return Convert.ToUInt16(o); }
catch { }
UInt16 result = default(UInt16);
if (!UInt16.TryParse(o.ToString(), out result))
result = _default;
return result;
}

public static int ToUInt16(object o)
{
return ToUInt16(o, default(int));
}

#endregion

#region UInt32

public static UInt32 ToUInt32(object o, UInt32 _default)
{
try { return Convert.ToUInt32(o); }
catch { }
UInt32 result = default(UInt32);
if (!UInt32.TryParse(o.ToString(), out result))
result = _default;
return result;
}

public static UInt32 ToUInt32(object o)
{
return ToUInt32(o, default(int));
}

#endregion

#region UInt64

public static UInt64 ToUInt64(object o, UInt64 _default)
{
try { return Convert.ToUInt64(o); }
catch { }
UInt64 result = default(UInt64);
if (!UInt64.TryParse(o.ToString(), out result))
result = _default;
return result;
}

public static UInt64 ToUInt64(object o)
{
return ToUInt64(o, default(int));
}

#endregion

#region Single / single
public static float ToSingle(object o, float _default)
{
try { return Convert.ToSingle(o); } catch { }
float result = default(float);
if (!float.TryParse(o.ToString(), out result))
result = _default;
return result;
}
public static float ToSingle(object o)
{
return ToSingle(o, default(float));
}
#endregion

#region Double / double
public static double ToDouble(object o, double _default)
{
try { return Convert.ToDouble(o); } catch { }
double result = default(double);
if (!double.TryParse(o.ToString(), out result))
result = _default;
return result;
}

public static double ToDouble(object o)
{
return ToDouble(o, default(double));
}
#endregion

#region Boolean / bool
/// <summary>
/// 转换为Bool类型
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static bool ToBoolean(object o, bool _default)
{
try { return Convert.ToBoolean(o); }
catch { }
bool result = default(bool);
if (!bool.TryParse(o.ToString(), out result))
result = _default;
return result;
}

public static bool ToBoolean(object o)
{
return ToBoolean(o, default(bool));
}
#endregion

#region Byte / byte
/// <summary>
/// 转换为byte类型
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static byte ToByte(object o, byte _default)
{
try { return Convert.ToByte(o); }
catch { }
byte result = default(byte);
if (!byte.TryParse(o.ToString(), out result))
result = _default;
return result;
}

public static byte ToByte(object o)
{
return ToByte(o, default(byte));
}
#endregion

#region Decimal / decimal
/// <summary>
/// 转换为decimal类型
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static decimal ToDecimal(object o, decimal _default)
{
try { return Convert.ToDecimal(o); }
catch { }
decimal result = default(decimal);
if (!decimal.TryParse(o.ToString(), out result))
result = _default;
return result;
}

public static decimal ToDecimal(object o)
{
return ToDecimal(o, default(decimal));
}
#endregion

#region DateTime / datetime
/// <summary>
/// 转换为DateTime类型
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static DateTime ToDateTime(object o, DateTime _default)
{
if (o == null)
{
return _default;
}

try { return Convert.ToDateTime(o); }
catch { }
DateTime result = DateTime.MinValue;
if (!DateTime.TryParse(o.ToString(), out result))
result = _default;
return result;
}

public static DateTime ToDateTime(object o)
{
return ToDateTime(o, DateTime.Parse("1900-01-01"));
}
#endregion

#region String / string
/// <summary>
/// 转换为String
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static string ToString(object o, string _default)
{
if (o == DBNull.Value)
{
return string.Empty;
}
return (o ?? _default).ToString();
}

public static string ToString(object o)
{
return ToString(o, string.Empty);
}
#endregion

public static decimal ToRound(decimal o)
{
return TypeHelper.ToInt((o / 10).ToString("f0")) * 10;
}
}

时间: 2024-08-05 00:11:46

常用的工具类2的相关文章

java中常用的工具类(二)

下面继续分享java中常用的一些工具类,希望给大家带来帮助! 1.FtpUtil Java 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

Android常用的工具类

主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java.目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils.PreferencesUtils.JSONUtils.FileUtils.ResourceUtils.StringUtils.ParcelUtils.RandomUtils.ArrayUtils.ImageUtils.ListUtils.MapUtils.ObjectUtils.SerializeUtils.S

common-lang 常用的工具类使用示例

原文:common-lang 常用的工具类使用示例 源代码下载地址:http://www.zuidaima.com/share/1550463718640640.htm common-lang 常用的工具类使用示例 StringUtil.dateUtil.DateFormatUtils.NumberUtils等 package com.zuidaima.trs.StringUtil; import java.io.File; import java.io.FileInputStream; imp

java中常用的工具类(三)

继续分享java中常用的一些工具类.前两篇的文章中有人评论使用Apache 的lang包和IO包,或者Google的Guava库.后续的我会加上的!谢谢支持IT江湖 一.连接数据库的综合类 Java 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

java中常用的工具类(一)

我们java程序员在开发项目的是常常会用到一些工具类.今天我汇总了一下java中常用的工具方法.大家可以在项目中使用.可以收藏!加入IT江湖官方群:383126909 我们一起成长 一.String工具类 Java 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 5

Android中常用的工具类01

1.图片和视频缩略图工具类 import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.media.ThumbnailUtils; /** * 缩略图生成工具类 * @author * */ public class ThumbnailGenerateUtils { private ThumbnailGenerateUtils(){}; /** * 根据指定的图像路径和大小来获取缩略图

Android中常用的工具类02

1.读取手机联系人信息 一般用在读取手机通讯录上传,这一块比较多. import java.util.ArrayList; import java.util.List; import android.content.ContentResolver; import android.content.Context; import android.database.Cursor; import android.provider.ContactsContract.CommonDataKinds.Phon

java并发编程中常用的工具类 Executor

/***************************************************  * TODO: description .  * @author: gao_chun  * @since:  2015-4-17  * @version: 1.0.0  * @remark: 转载请注明出处  **************************************************/ java.util.concurrent.Executor 使用 Execut

28个Java常用的工具类

源码下载:http://pan.baidu.com/s/1pJLSczD Base64.javaBase64DecodingException.javaCConst.javaCharTools.javaConfigHelper.javaCounter.javaCTool.javaDateHandler.javaDateUtil.javaDealString.javaDebugOut.javaDom4jHelper.javaEscape.javaExecHelper.javaFileHelper.

Java语言Lang包下常用的工具类介绍_java - JAVA

文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 无论你在开发哪中 Java 应用程序,都免不了要写很多工具类/工具函数.你可知道,有很多现成的工具类可用,并且代码质量都很不错,不用你写,不用你调试,只要你发现. 在 Apache Jakarta Common 中, Lang 这个 Java 工具包是所有 Apache Jakarta Common 项目中被使用最广泛的,几乎你所知道的名气比较大的软件里面都有用到它,包括 Tomcat, Weblogic, Webs