Math.round(11.5)等于()Math.round(-11.5)等于()

几天前去面试,这道简单的题目居然做错了,看来基础就是慢慢积累的。并不断使用和复习才会成为高手,假设基础不是那么熟练。恐怕在成为高手的路上会困难重重。所以在做项目的间歇时间。偶尔回顾一下最基础的知识。是一个比較好的投资。

好了。以下介绍的就是Math类中三个与取整有关的方法 :

1、Math.round();//四舍五入

2、Math.ceil();//向上取整

3、Math.floor();//向下取整

代码展示:

public class TextOne {
	public static void main(String[] args) {
		/**
		 * 四舍五入取整
		 */
		System.out.println(Math.round(11.5));
		System.out.println(Math.round(-11.5));
		
		/**
		 *向上取整 
		 */
		System.out.println(Math.ceil(11.5));
		System.out.println(Math.ceil(-11.5));
		
		/**
		 * 向下取整
		 */
		System.out.println(Math.floor(11.5));
		System.out.println(Math.floor(-11.5));
		
	}
}

执行结果:

12
-11
12.0
-11.0
11.0
-12.0





 

原文地址:https://www.cnblogs.com/zfyouxi/p/5209113.html