math.ceil函数 java_java基础之二:取整函数(Math类)

在日常开发中经常会遇到数字的情况,有关数据的场景中会遇到取整的情况,java中提供了取整函数。看下java.lang.Math类中取整函数的用法。

一、概述

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

二、详述

ceil()方法

ceil方法的作用是向上取整。下面看方法的定义,

cdbf09917ecb8b8cff48bd6b5964066d.png

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

正数情况

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

packagecom.example.demo.test;public classTestMathCeilPost {public static voidmain(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);

}

}

看执行结果,

47506a8b9a19d946b3c7cbb6afcd006d.png

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

负数情况

看负数的情况,

packagecom.example.demo.test;public classTestMathCeilNegative {public static voidmain(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);

}

}

看执行结果,

052dd7eca621bcaf21bc66c4647c1cca.png

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

floor()

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

aa0705dc696992218df4a028791d78a7.png

正数情况

看正数情况下,

packagecom.example.demo.test;public classTestMathFloorPost {public static voidmain(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);

}

}

看执行结果,

5171b2faf695cfabb0278426e51d1f34.png

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

负数情况

看负数情况下,

packagecom.example.demo.test;public classTestMathFloorNegative {public static voidmain(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);

}

}

看执行结果,

49b1da55d75972b1a2ffa0bcbe8bc1d4.png

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

round()

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

d640485bcdae7ea63ae50c3cc96a8d73.png

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

正数情况

看正数的情况,

packagecom.example.demo.test;public classTestMathRoundPost {public static voidmain(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);

}

}

看执行结果,

e971a4a8d889aab1feac30d0723b0729.png

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

负数情况

看负数情况,

packagecom.example.demo.test;public classTestMathRoundNegative {public static voidmain(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);

}

}

看执行结果,

7c7fcaf4d5d58a433eac7f9fdfadf599.png

从上面的结果可以看到,在负数情况下不是简单的“四舍五入”,而要考虑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 round(-12.6)-->-13

有不正之处,欢迎指正,感谢!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值