Math类的常用方法比较
2013-07-18 21:00
162 查看
ath类中提供了三个与取整有关的方法:ceil、floor、round,这些方法的作用与它们的英文名称的含义相对应,例如,ceil的英文意义是天花板,该方法就表示向上取整,Math.ceil(11.3)的结果为12,Math.ceil(-11.3)的结果是-11;floor的英文意义是地板,该方法就表示向下取整,Math.ceil(11.6)的结果为11,Math.ceil(-11.6)的结果是-12;最难掌握的是round方法,它表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整,加完之后刚好是整数的不用动,那个值即为结果,所以,Math.round(11.5)的结果为12,Math.round(-11.5)的结果为-11。
System.out.println(Math.round(11.5)); // System.out.println(Math.round(11.4)); //11 System.out.println(Math.round(-11.5)); //-11 System.out.println(Math.round(-11.4)); //-11 System.out.println(Math.round(-11.6));//-12
相关文章推荐
- Python合并两个字典的常用方法与效率比较
- jquery和javascript的区别(常用方法比较)
- Java中Math类常用方法
- Java中Math类的常用方法
- jquery和javascript的区别(常用方法比较)
- JavaScript 与JQuery 常用方法比较
- 比较常用的Properties配置文件的使用方法示例
- Math类常用的常量和方法
- ios 开发,通讯录信息调用常用方法,这个比较全,不用再整理了
- 常用WebServices返回数据的4种方法比较 (转)
- RecyclerView 的常用方法;工作原理与ListView比较;源码解析
- java中常用的字符串的比较方法(两种)
- java两个比较常用的工具方法代码
- C# 比较常用的方法总结
- Java 中队列的使用示例及常用方法示例和比较
- ios 开发,通讯录信息调用常用方法,这个比较全,不用再整理了;
- 设计技巧33:从方法中获取信息 Collecting Parameter:集合参数,从方法中获取信息 容器比较常用
- Python数据分析几个比较常用的方法
- 比较有用的常用dom对面属性&方法
- 数据结构和算法分析之排序篇--归并排序(Merge Sort)和常用排序算法时间复杂度比较(附赠记忆方法)