java基础之二:取整函数(Math类) |
您所在的位置:网站首页 › java最大的整数类型 › java基础之二:取整函数(Math类) |
在日常开发中经常会遇到数字的情况,有关数据的场景中会遇到取整的情况,java中提供了取整函数。看下java.lang.Math类中取整函数的用法。 一、概述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); } }看执行结果, 通过上面的结果,可以看到在正数情况下是向上取整,也就是大于原始结果的最小的正数, 负数情况看负数的情况, 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); } }看执行结果, 可以看出也是取大于给定数的最小的负整数。 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); } }看执行结果, 通过上面的结果,可以看到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); } }看执行结果, 通过上面的结果,可以看到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); } }看执行结果, 看执行结果就是进行数学上的四舍五入运算,得到结果。 负数情况看负数情况, 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); } }看执行结果, 从上面的结果可以看到,在负数情况下不是简单的“四舍五入”,而要考虑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
有不正之处,欢迎指正,感谢! |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |