当我们在编写程序时,计算是不可避免的,取整更是常见,下面是对Math中关于取整函数的一个小梳理。
Math类中提供了三个与取整有关的方法,即ceil、floor和round。它们函数作用和其英文意义差不多对应,ceil是天花板的意思,该方法就表示向上取整,floor是地板的意思,该方法就表示向下取整,而round有点麻烦,是银行家舍入算法,即四舍六入五取偶。
1、ceil(取天板值,对一个数进行上取整)
Math.Ceiling(0.0)=0
Math.Ceiling(0.1)=1
Math.Ceiling(0.2)=1
Math.Ceiling(0.9)=1
Math.Ceiling(1.0)=1
Math.Ceiling(1.1)=2
Math.Ceiling(-1.1)=-1
Math.Ceiling(-1.2)=-1
......
2、floor(取地板值,对一个数进行下取整)
Math.Floor(0.0)=0
Math.Floor(0.1)=0
Math.Floor(0.2)=0
Math.Floor(0.9)=0
Math.Floor(-1.1)=-2
Math.Floor(-1.8)=-2
......
3、round(四舍六入五取偶)
Math.Round(0.4)=0
Math.Round(0.5)=0
Math.Round(0.6)=1
Math.Round(0.4)=0
Math.Round(2.5)=2
Math.Round(3.5)=4
Math.Round(4.5)=4
Math.Round(5.5)=6
......
这是对取整函数的简单整理,很好理解,希望对大家以后的数值计算有所帮助。