Java关于Math类的三个取整方法

Math类中的三大取整函数:

1.ceil(返回double类型)

2.floor(返回double类型)

3.round(返回int类型)


1.ceil:意思是天花板,java中叫做向上取整,大于等于该数字的最接近的整数

例:
Math.ceil(10.2)=11.0

Math.ceil(-10.2)=-10.0

Math.ceil(10.7)=11.0

Math.ceil(-10.2)=-10.0


2.floor:意思是地板,java中叫做向下取整,小于等于该数字的最接近的整数

例:

Math.floor(10.2)=10.0

Math.floor(-10.2)=-11.0

Math.floor(10.7)=10.0

Math.floor(-10.2)=-11.0


3.round:就是四舍五入

例:
Math.round(10.2)=10

Math.round(-10.2)=-10

Math.round(10.5)=11

Math.round(-10.5)=-10

Math.round(-10.7)=-11

posted @ 2017-10-18 21:44  ·卿欢·  阅读(840)  评论(0编辑  收藏  举报