扩展Decimal的ToString方法,或者ToInt方法,新建一个Class文件,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Text.RegularExpressions;
namespace Demo.BLL
{
/// <summary>
/// 扩展方法类
/// </summary>
public static class FuncExtend
{
public static string ToStringC(this decimal obj)
{
if (obj != null)
{
return obj.ToString("0.00");
}
else
{
return "0.00";
}
}
/// <summary>
/// decimal?转为int?
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static int? ToInt(this Decimal? obj)
{
checked
{
if (obj.HasValue)
{
return (int)obj.Value;
}
else
{
return null;
}
}
}
public static IList<T> Clone<T>(this IList<T> source) where T:ICloneable
{
if (source == null)
{
return null;
}
IList<T> newList = new List<T>(source.Count);
foreach (T tmp in source)
{
newList.Add((T)((ICloneable)tmp.Clone()));
}
return newList;
}
}
}
归纳如下:
1.建一个static class
2.建static返回值的方法,传参用this decimal 对象等。使用this关键字。
在页面中直接使用。
decimal abc = 0;
string result = abc.ToStringC();