摘选自菜鸟教程,我看着很有意思,所以自己写下来让我可以复习一下。

1.floor、round、ceil含义:

floor:返回的值是小于于等于(<=)给定参数的最小的整数。意思么就是只能比自己本身小,比如说1.8和2.2,如果使用的floor,就只能变成1.0和2.0;

ceil:返回的值是大于于等于(>=)给定参数的最小的整数。意思么就是能比自己本身大,比如说1.8和2.2,如果使用的floor,就能变成2.0和3.0;

round:四舍五入,它的算法为:math.floor(x+0.2)。用4个数来举例:1.1、1.6、-1.3、-1.8使用round后,第一步为:1.6、2.1、-0.8、-1.3.那么在使用floor:1、2、-1、-2

注意:floor和ceil是浮点数而不是整型,只有round为整型

附上代码:

package come.cute.card;
public class SSSS {
    public static void main(String[] args) {
         // TODO Auto-generated method stub
         double[] nums = { 1.8,2.2,1.1,1.6,-1.3,-1.8 };   
         for (double num : nums) {   
           test(num);   
         }   
       }   
       
       public static void test(double num) {   
         System.out.println("Math.floor(" + num + ")=" + Math.floor(num));   
         System.out.println("Math.round(" + num + ")=" + Math.round(num));   
         System.out.println("Math.ceil(" + num + ")=" + Math.ceil(num));   
       }   
     }

结果:

Math.floor(1.8)=1.0
 Math.round(1.8)=2
 Math.ceil(1.8)=2.0
 Math.floor(2.2)=2.0
 Math.round(2.2)=2
 Math.ceil(2.2)=3.0
 Math.floor(1.1)=1.0
 Math.round(1.1)=1
 Math.ceil(1.1)=2.0
 Math.floor(1.6)=1.0
 Math.round(1.6)=2
 Math.ceil(1.6)=2.0
 Math.floor(-1.3)=-2.0
 Math.round(-1.3)=-1
 Math.ceil(-1.3)=-1.0
 Math.floor(-1.8)=-2.0
 Math.round(-1.8)=-2
 Math.ceil(-1.8)=-1.0