您的位置:首页 > 编程语言 > C#

C#中有趣的的四舍五入现象和一个简单的解决方案

2008-01-15 15:11 309 查看
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# string