java中如何取整?

Python012

java中如何取整?,第1张

有float类型的

向上取整:Math.ceil() //只要有小数都+1

向下取整:Math.floor() //不取小数

四舍五入:Math.round() //四舍五入

有float类型的\x0d\x0a\x0d\x0a向上取整:Math.ceil() //只要有小数都+1\x0d\x0a向下取整:Math.floor() //不取小数\x0d\x0a四舍五入:Math.round() //四舍五入

(要学会百度啊)

Math类中提供的三个与取整有关的方法:

第一个: ceil

ceil的意思就是: 天花板的意思该方法表示的是向上取整Math.ceil(11.3)的值是12 Math.ceil.(-11.6)的结果是-11

第二个是: floor

首先他的英文含义就是地板的含义,该方法就表示的是向下取整, 

Math.floor(11.6)的结果就是11  

Math.floor(-11.4)的结果就是-12

第三个是: round

他表示的是四舍五入,算法为 Math.floor(x+0.5)也就是在原来的数字上加上0.5之后再进行向下取整 

Math.round(11.5)也就是 Math.floor(11.5+0.5)= Math.floor(12)=12 

同理: Math.round(-11.5)= Math.floor(-11.5+0.5)=Math.floor(-11.0)= -11

原文地址:网页链接