Math总结心得
2018-08-30 本文已影响0人
大山村长
在日常开发中,经常会使用到一些数据使用JDK自带的Math进行运算的问题;下面拿几个Math的方法做一下总结:random、round、max、min、ceil、abs等等
random api文档原话:Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.即返回一个带正符号的双值,大于或等于0.0,小于1.0。
round:返回与参数最接近的long值,将其四舍五入到正无穷。
System.out.println(Math.round(-11.5));
System.out.println(Math.round(11.5));
结果:
-11
12
round取正,可以这样理解,X+0.5然后向下取正
ceil与floor相反,ceil是向上取整,floor是向下取整。如
![](https://img.haomeiwen.com/i11710646/559735f23609d4ed.png)
结果
![](https://img.haomeiwen.com/i11710646/336dd42817cfcb58.png)
max与min相反,去两个数中最大值,而abs是取绝对值。如
![](https://img.haomeiwen.com/i11710646/3052ebb1912ca1d7.png)
![](https://img.haomeiwen.com/i11710646/327f8e05c2d0a1ee.png)
Math还有很多方法提供,由于api说明已经很明白,就不仔细说了,以上是由于在参与笔试时i,本人忘了用法,因此特意写一下文章记录一下,增加印象,大家如有需要,可借鉴在线文档说明:https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html