首页 > 试题广场 >

What is displayed when the fol

[单选题]
下面代码输出是?
double d1=-0.5;
System.out.println("Ceil d1="+Math.ceil(d1));
System.out.println("floor d1="+Math.floor(d1));


  • Ceil d1=-0.0
    floor d1=-1.0
  • Ceil d1=0.0
    floor d1=-1.0
  • Ceil d1=-0.0
    floor d1=-0.0
  • Ceil d1=0.0
    floor d1=0.0
  • Ceil d1=0
    floor d1=-1
推荐
A
ceil:大于等于 x,并且与它最接近的整数。
floor:小于等于 x,且与 x 最接近的整数。
编辑于 2015-02-04 18:00:01 回复(12)
这里主要是有一点:
Math.ceil(d1) 
ceil 方法上有这么一段注释:If the argument value is less than zero but greater than -1.0, then the result is negative zero
如果参数小于0且大于-1.0,结果为 -0
Math.floor(d1)
ceil 和 floor 方法 上都有一句话:If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as  the argument,意思为:如果参数是 NaN、无穷、正 0、负 0,那么结果与参数相同,
如果是 -0.0,那么其结果是 -0.0
编辑于 2016-04-13 09:01:15 回复(25)
ceil:天花板数,向上取整。
floor:地板数,向下取整
发表于 2015-10-01 23:53:29 回复(14)
Ceil d1=-0.0
floor d1=-1.0
发表于 2018-11-06 19:56:35 回复(0)
ceiling有上限,天花板的意思,也就是说是比这个数大的 floor地板,可以理解成稍微低一些,也就是说比这个数小的 但是俩个范围都不能超过1,且是个整数。
发表于 2016-12-15 01:57:04 回复(0)
Returns the smallest (closest to negative infinity)
     * {@code double} value that is greater than or equal to the
     * argument and is equal to a mathematical integer. Special cases:
     * <ul><li>If the argument value is already equal to a
     * mathematical integer, then the result is the same as the
     * argument.  <li>If the argument is NaN or an infinity or
     * positive zero or negative zero, then the result is the same as
     * the argument.  <li>If the argument value is less than zero but
     * greater than -1.0, then the result is negative zero.</ul> Note
     * that the value of {@code Math.ceil(x)} is exactly the
     * value of {@code -Math.floor(-x)}.
ceil()方法源码注释:大体意思是 (请英文好的来翻译) negative  负
发表于 2016-03-08 10:39:03 回复(0)
符号不变,类型不变
发表于 2015-10-03 17:53:34 回复(2)
ceil()天花板数,向上取整 floor()地板数,向下取整 保持数据符号不变,类型不变
发表于 2016-03-11 08:47:00 回复(9)
floor 返回不大于的最大整数 
round 则是4舍5入的计算,入的时候是到大于它的整数
round方法,它表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整,所以,Math.round(11.5)的结果为12,Math.round(-11.5)的结果为-11。
ceil 则是不小于他的最小整数

发表于 2015-04-15 14:39:19 回复(0)

java API

public static double ceil(double a)
返回最小的(最接近负无穷大)double值,该值大于或等于参数,并且等于某个整数。特殊情况是:
  • 如果参数值总是等于某个整数,那么结果与该参数相同。
  • 如果参数是 NaN、无穷大、正零或负零,那么结果与参数相同。
  • 如果参数值小于零,但是大于 -1.0,那么结果是负零。
注意,Math.ceil(x)的值与-Math.floor(-x)的值完全相同。



参数: a- 一个值。返回: 最小(最接近负无穷大)浮点值,该值大于或等于该参数,并且等于某个整数。
发表于 2015-12-17 22:11:41 回复(0)
这样子记:

ceil:天花板 大于等于x的最接近整数,向上取整
floor:地板 小于等于x的最接近整数,向下取整

要注意的是,两者返回的都是double类型!
发表于 2016-08-31 14:05:31 回复(1)
答案:A
这分别是向上取整和向下取整
正数如5.5,向上取整为6.0,向下取整为5.0
负数如本题的-0.5,向上取整为-0.0,向下取整为-1.0
发表于 2015-01-26 19:52:00 回复(2)
记住它们的英文就可以。
ceil:天花板,即向上取整。
floor:地板,即向下取整。
值得一提的是:二者返回的都是浮点型。
至于为什么是-0.0,而不是0.0,因为源码注释:“If the argument value is less than zero but greater than -1.0, then the result is negative zero ”,翻译过来就是:如果参数是-1.0到0.0之间的数,结果是-0.0。

更多参见:http://blog.csdn.net/qq_27258799/article/details/51854911
发表于 2016-07-10 11:34:57 回复(0)
math.ceil和math.floor返回值类型都是double
math.round有重载,返回值类型可以是long或int
发表于 2017-07-26 10:54:56 回复(0)
0还有负数,见识了
发表于 2019-10-04 20:04:38 回复(1)
Math.ceil()执行向上舍入,即它总是将数值向上舍入为最接近的整数;
◎Math.floor()执行向下舍入,即它总是将数值向下舍入为最接近的整数;
◎Math.round()执行标准舍入,即它总是将数值四舍五入为最接近的整数(这也是我们在数学课上学到的舍入规则)。
发表于 2018-09-03 16:39:03 回复(0)
总结一下:
Math类中提供了三个与取整有关的方法:ceil,floor,round,这些方法的作用于它们的英文名称的含义相对应,
例如: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)的结果是12,Math.round(-11.5)的结果为-11.

发表于 2017-11-29 13:54:13 回复(1)
Math.ceil():ceil为天花板的意思,向上取整,返回值类型为double(public static double ceil(double a)),符号不变
Math.floor():floor为地板的意思,向下取整,返回值类型为double(public static double floor(double a)),符号不变
Math.round(double a):加上0.5,再向下取整-------(long)Math.floor(a + 0.5d),返回最接近参数的long
Math.round(float a):加上0.5,再向下取整-------(int)Math.floor(a + 0.5f),返回最接近参数的int
以上为自己的理解,仅供参考

编辑于 2016-10-06 09:20:01 回复(0)
ceil:大于等于 x,并且与它最接近的整数。
floor:小于等于 x,且与 x 最接近的整数。
发表于 2015-09-18 13:26:39 回复(0)
我个人的理解,浮点数的向上取整与向下取整是分区间的,负无穷到-0.0以及0.0到正无穷,处在不同区间的浮点数向上取整和向下取整对应的数值在自己所在的区间,两个方法返回的都是浮点数。看了一下jdk1.8的源码没怎么看明白。-0.0与0.0的区别在于符号位的不同。
发表于 2022-07-09 18:43:29 回复(0)
Ceil d1=-0.0  这是我没想到的哈哈哈,我以为就是0.0
发表于 2022-04-26 15:17:52 回复(0)