/// <summary>
/// 实现数据的四舍五入法
/// </summary>
/// <param name="v">要进行处理的数据</param>
/// <param name="x">保留的小数位数</param>
/// <returns>四舍五入后的结果</returns>
public decimal Round(decimal v, int x)
{
var isNegative = false;
//如果是负数
if (v < )
{
isNegative = true;
v = -v;
} var IValue = ;
for (var i = ; i <= x; i++)
{
IValue = IValue * ;
} var Int = Math.Round(v * IValue, );
v = Int / IValue; if (isNegative)
{
v = -v;
} return v;
} /// <summary>
/// 实现数据的向上取整
/// </summary>
/// <param name="v">要进行处理的数据</param>
/// <param name="x">保留的小数位数</param>
/// <returns>向上取整后的结果</returns>
public decimal Ceiling(decimal v, int x)
{
var isNegative = false;
//如果是负数
if (v < )
{
isNegative = true;
v = -v;
} var IValue = ;
for (var i = ; i <= x; i++)
{
IValue = IValue * ;
} var Int = Math.Ceiling(v * IValue);
v = Int / IValue; if (isNegative)
{
v = -v;
} return v;
} /// <summary>
/// 实现数据的向下取整
/// </summary>
/// <param name="v">要进行处理的数据</param>
/// <param name="x">保留的小数位数</param>
/// <returns>向下取整后的结果</returns>
public decimal Floor(decimal v, int x)
{
var isNegative = false;
//如果是负数
if (v < )
{
isNegative = true;
v = -v;
} var IValue = ;
for (var i = ; i <= x; i++)
{
IValue = IValue * ;
} var Int = Math.Floor(v * IValue);
v = Int / IValue; if (isNegative)
{
v = -v;
} return v;
} /// <summary>
/// 实现见角进元
/// </summary>
/// <param name="v">要进行处理的数据</param>
/// <param name="x">保留的小数位数</param>
/// <returns>见角进元后的结果</returns>
public decimal SeeDownUp(decimal v, int x)
{
var isNegative = false;
//如果是负数
if (v < )
{
isNegative = true;
v = -v;
} var IValue = ;
for (var i = ; i <= x; i++)
{
IValue = IValue * ;
} var stirngpanduan = Math.Floor(v * IValue * ).ToString();
if (stirngpanduan.Substring(stirngpanduan.Length - ) == "")
{
var Int = Math.Floor(v * IValue);
v = Int / IValue;
}
else
{
var Int = Math.Ceiling(v * IValue);
v = Int / IValue;
} if (isNegative)
{
v = -v;
} return v;
} /// <summary>
/// 实现数据的四舍五入,向上取整,向下取整
/// </summary>
/// <param name="v">要进行处理的数据</param>
/// <param name="x">保留的小数位数</param>
/// <param name="type">1四舍五入,2向上取整,3向下取整</param>
/// <returns>decimal</returns>
public decimal DecimalProcessing(decimal v, int x, string type)
{
decimal result = ;
switch (type)
{
case "":
result = Round(v, x);
break;
case "":
result = Ceiling(v, x);
break;
case "":
result = Floor(v, x);
break;
case "":
result = SeeDownUp(v, x);
break;
default:
result = Round(v, x);
break;
} return result;
}