static void Main(string[] args)
{
decimal dValue = 400.5m;
Console.WriteLine(Math.Round(dValue,0).ToString());
Console.WriteLine(RoundDecimal(dValue).ToString());
dValue = 40.5m;
Console.WriteLine(Math.Round(dValue,0).ToString());
Console.WriteLine(RoundDecimal(dValue).ToString());
dValue = 44.5m;
Console.WriteLine(Math.Round(dValue,0).ToString());
Console.WriteLine(RoundDecimal(dValue).ToString());
dValue = 4.5m;
Console.WriteLine(Math.Round(dValue,0).ToString());
Console.WriteLine(RoundDecimal(dValue).ToString());
Console.Read();
}
public static decimal RoundDecimal(decimal dValue)
{
return dValue.ToString().Split('.').Length == 2 && Convert.ToDecimal(dValue.ToString().Split('.')[0]) % 4 == 0 ?( Convert.ToDecimal(dValue.ToString().Split('.')[1]) == 5 ?Convert.ToDecimal(dValue.ToString().Split('.')[0]) + 1:Math.Round(dValue,0)):Math.Round(dValue,0);
}
输出结果如下:
本文介绍了一种特殊的四舍五入方法实现,该方法在C#中使用decimal类型来处理数值,并通过自定义函数RoundDecimal实现了特定条件下的四舍五入逻辑。
5万+

被折叠的 条评论
为什么被折叠?



