一点小总结,与大家分享:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//Floor向负无穷方向舍入为最接近的整数
Console.WriteLine(decimal .Floor(-1.3m)); //-2
Console.WriteLine(decimal .Floor (3.5m)); //3
Console.WriteLine(decimal .Floor (4m)); //4
{
class Program
{
static void Main(string[] args)
{
//Floor向负无穷方向舍入为最接近的整数
Console.WriteLine(decimal .Floor(-1.3m)); //-2
Console.WriteLine(decimal .Floor (3.5m)); //3
Console.WriteLine(decimal .Floor (4m)); //4
//Truncate向零方向舍入为整数
Console.WriteLine(decimal.Truncate(-1.3m));//-1
Console.WriteLine(decimal.Truncate (3.5m));//3
Console.WriteLine(decimal.Truncate (4m)); //4
//如果想实现四舍五入,则必须用下面的技巧,保留到小数点后2位,就用100,保留到小数点后3位就用1000,依次类推
decimal a = 8.335m,b=8.345m;
Console.WriteLine(decimal .Truncate (a*100+0.5m)/100); //8.34
Console.WriteLine (decimal .Truncate (b*100+0.5m)/100); //8.35
Console.WriteLine(decimal.Truncate(-1.3m));//-1
Console.WriteLine(decimal.Truncate (3.5m));//3
Console.WriteLine(decimal.Truncate (4m)); //4
//如果想实现四舍五入,则必须用下面的技巧,保留到小数点后2位,就用100,保留到小数点后3位就用1000,依次类推
decimal a = 8.335m,b=8.345m;
Console.WriteLine(decimal .Truncate (a*100+0.5m)/100); //8.34
Console.WriteLine (decimal .Truncate (b*100+0.5m)/100); //8.35
//总结,System.Double(double)和System.Single(float)结构都没有这2种方法,所以一般用decimal来实现取整
}
}
}
}
}
}
本文转自terryli51CTO博客,原文链接:http://blog.51cto.com/terryli/521541 ,如需转载请自行联系原作者