15

一、概述
java.lang.Math类中有三个和取整相关的函数,分别是ceil()、floor()、round()方法。所谓取整就是舍弃小数位,保留整数位,但是如何舍弃和保留每个方法的处理方式不一样,看其具体用法。

二、详述
ceil()方法
ceil方法的作用是向上取整。下面看方法的定义,

接收一个double类型的参数,返回double类型。

正数情况
下面看参数为正数的情况,ceil是如何取整的,

复制代码
package com.example.demo.test;

public class TestMathCeilPost {
public static void main(String[] args) {

    //定义double类型
    double b=12.5;
    double b2=12.1;

    //向上取整
    double d=Math.ceil(b);
    double d2=Math.ceil(b2);
    //转化为int类型
    int a=Double.valueOf(d).intValue();
    int a2=Double.valueOf(d2).intValue();

    System.out.println(b+"调用Math.ceil方法后的值为:"+a);
    System.out.println(b2+"调用Math.ceil方法后的值为:"+a2);




}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

}
复制代码
看执行结果,

通过上面的结果,可以看到在正数情况下是向上取整,也就是大于原始结果的最小的正数,

负数情况
看负数的情况,

复制代码
package com.example.demo.test;

public class TestMathCeilNegative {
public static void main(String[] args) {

    //定义double类型
    double b=-12.5;
    double b2=-12.1;

    //向上取整
    double d=Math.ceil(b);
    double d2=Math.ceil(b2);
    //转化为int类型
    int a=Double.valueOf(d).intValue();
    int a2=Double.valueOf(d2).intValue();

    System.out.println(b+"调用Math.ceil方法后的值为:"+a);
    System.out.println(b2+"调用Math.ceil方法后的值为:"+a2);


}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

}
复制代码
看执行结果,

可以看出也是取大于给定数的最小的负整数。

floor()
floor方法的作用是向下取整,看方法定义如下,

正数情况
看正数情况下,

复制代码
package com.example.demo.test;

public class TestMathFloorPost {
public static void main(String[] args) {

    //定义double类型
    double b=12.5;
    double b2=12.1;

    //向下取整
    double d=Math.floor(b);
    double d2=Math.floor(b2);
    //转化为int类型
    int a=Double.valueOf(d).intValue();
    int a2=Double.valueOf(d2).intValue();

    System.out.println(b+"调用Math.floor方法后的值为:"+a);
    System.out.println(b2+"调用Math.floor方法后的值为:"+a2);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

}
复制代码
看执行结果,

通过上面的结果,可以看到floor方法在正数情况下,是取小于给定数的最大的正数。

负数情况
看负数情况下,

复制代码
package com.example.demo.test;

public class TestMathFloorNegative {
public static void main(String[] args) {

    //定义double类型
    double b=-12.5;
    double b2=-12.1;

    //向下取整
    double d=Math.floor(b);
    double d2=Math.floor(b2);
    //转化为int类型
    int a=Double.valueOf(d).intValue();
    int a2=Double.valueOf(d2).intValue();

    System.out.println(b+"调用Math.floor方法后的值为:"+a);
    System.out.println(b2+"调用Math.floor方法后的值为:"+a2);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

}
复制代码
看执行结果,

通过上面的结果,可以看到floor方法在负数情况下,是取小于给定数的最大的正数。

round()
round方法的作用是“四舍五入”,看方法定义,

看到方法的入参有float、double,出参对应这int、long。以double为例。

正数情况
看正数的情况,

复制代码
package com.example.demo.test;

public class TestMathRoundPost {
public static void main(String[] args) {

    //定义double类型
    double b=12.5;
    double b2=12.1;

    //向上取整
    double d=Math.round(b);
    double d2=Math.round(b2);
    //转化为int类型
    int a=Double.valueOf(d).intValue();
    int a2=Double.valueOf(d2).intValue();

    System.out.println(b+"调用Math.round方法后的值为:"+a);
    System.out.println(b2+"调用Math.round方法后的值为:"+a2);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

}
复制代码
看执行结果,

看执行结果就是进行数学上的四舍五入运算,得到结果。

负数情况
看负数情况,

复制代码
package com.example.demo.test;

public class TestMathRoundNegative {
public static void main(String[] args) {

    //定义double类型
    double b=-12.6;
    double b2=-12.1;
    double b3=-12.5;


    double d=Math.round(b);
    double d2=Math.round(b2);
    double d3=Math.round(b3);
    //转化为int类型
    int a=Double.valueOf(d).intValue();
    int a2=Double.valueOf(d2).intValue();
    int a3=Double.valueOf(d3).intValue();

    System.out.println(b+"调用Math.round方法后的值为:"+a);
    System.out.println(b2+"调用Math.round方法后的值为:"+a2);
    System.out.println(b3+"调用Math.round方法后的值为:"+a3);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

}
复制代码
看执行结果,

从上面的结果可以看到,在负数情况下不是简单的“四舍五入”,而要考虑XXX.5的情况,小数位上的数字小于等于5,舍弃小数位,保留整数位数;小数位上的数字大于5,舍弃小数位且加-1;

三、总结
本文分析了Math类中的ceil、floor、round函数,

ceil

正数和负数情况下都是向上取整,这里的向上指的是取大于给定数字的最小整数,例,ceil(12.3)–>13.0 ceil(-12.6)–>-12.0

floor

正数和负数情况下都是向下取整,这里的向下指的是取小于给定数字的最大整数,例,floor(12.3)–>12.0 floor(-12.6)–>-13.0

round

正数情况下是数学上的四舍五入,例,round(12.3)–>12 round(12.5)–>13

正数情况下需要注意小数位的数字,小于等于5,则舍去小数位;大于5,则加-1,例,round(-12.3)–>-12 round(-12.5)–>-12

posted @ 2020-12-31 11:48  远道a  阅读(85)  评论(0编辑  收藏  举报