public class Test {
public static void main(String[] args) {
double d1 = 3.4, d2 = 3.6; //正数
double d3 = -3.4, d4 = -3.6; //负数 float f1 = 4.4F, f2 = 4.6F; //正数
float f3 = -4.4F, f4 = -4.6F; //负数 //floor()方法只能接收double类型,返回double类型
//向下取整,返回小于参数的最大整数
System.out.println(Math.floor(d1));//3.0
System.out.println(Math.floor(d2));//3.0
System.out.println(Math.floor(d3));//-4.0
System.out.println(Math.floor(d4));//-4.0 //ceil()方法只能接收double类型,返回double类型
//向上取整,返回大于参数的最小整数
System.out.println(Math.ceil(d1));//4.0
System.out.println(Math.ceil(d2));//4.0
System.out.println(Math.ceil(d3));//-3.0
System.out.println(Math.ceil(d4));//-3.0 //round()方法可以接收double类型,返回long类型
//表示“四舍五入”,算法为Math.floor(x+0.5),即将参数加上0.5后再向下取整
System.out.println(Math.round(d1));//
System.out.println(Math.round(d2));//
System.out.println(Math.round(d3));//-3
System.out.println(Math.round(d4));//-4 //round()方法可以接收float类型,返回int类型
System.out.println(Math.round(f1));//
System.out.println(Math.round(f2));//
System.out.println(Math.round(f3));//-4
System.out.println(Math.round(f4));//-5
}
}

Math类的三个方法比较: floor() ceil() round()的更多相关文章

  1. Java关于Math类的三个取整方法

    0x01 在java的Math类中有三个关于浮点数取整数的方法,分别是ceil (向上取整) floor(向下取整) round(四舍五入) 三个方法 0x02 ceil 向上取整,取整后总是比原来的 ...

  2. [转]Javascript定义类的三种方法

    作者: 阮一峰 原文地址:http://www.ruanyifeng.com/blog/2012/07/three_ways_to_define_a_javascript_class.html 将近2 ...

  3. matlab中fix, floor, ceil, round 函数的使用方法

    转载: https://www.ilovematlab.cn/thread-91895-1-1.html Matlab取整函数有: fix, floor, ceil, round.具体应用方法如下: ...

  4. MATLAB中取整函数(fix, floor, ceil, round)的使用

    MATLAB取整函数 1)fix(x) : 截尾取整. >> fix( [3.12 -3.12]) ans = 3    -3(2)floor(x):不超过x 的最大整数.(高斯取整) & ...

  5. paper 68 :MATLAB中取整函数(fix, floor, ceil, round)的使用

    MATLAB取整函数 1)fix(x) : 截尾取整. >> fix( [3.12 -3.12]) ans =      3    -3 (2)floor(x):不超过x 的最大整数.(高 ...

  6. java数学函数Math类中常用的方法

    Math类提供了常用的一些数学函数,如:三角函数.对数.指数等.一个数学公式如果想用代码表示,则可以将其拆分然后套用Math类下的方法即可. Math.abs(12.3);               ...

  7. C#的math类的全部运算方法

    Abs 返回指定数字的绝对值.Acos 返回余弦值为指定数字的角度.Asin 返回正弦值为指定数字的角度.Atan 返回正切值为指定数字的角度.Atan2 返回正切值为两个指定数字的商的角度.BigM ...

  8. mathlab之floor,ceil,round,int以及fix函数

    建议自己动手敲敲,网上很多人自己都没搞清楚然后好多错的.毕竟自己亲眼看到结果才有说服力. 以下是我亲眼见到的结果. 1.double floor(double)函数 floor()函数是常用的取整函数 ...

  9. 关于Math类的round、floor、ceil三个方法

    一.Math类这三个方法的简介 1.round():取最接近的值. 对于这个方法,查看源代码,其实现如下: public static long round(double a) { if (a != ...

随机推荐

  1. [转]Spring 之 Log4j 的配置

  2. mysql错误:Can’t create TCP/IP socket (10106) 解决方法

    错误描述 “mysql错误:Can’t create TCP/IP socket (10106)”,目测是socket端口被占用的原因,然后在打开tomcat,报的错误中也包含了“socket”,再一 ...

  3. centos7.3下apache搭建django[未成功]

    1 apache肯定已经按照完毕了, 如果没有  yum install httpd yum install mod_wsgi 安装完成之后,mod_wsgi.so会在Apache的modules目录 ...

  4. mybatis 学习五 二级缓存不推荐使用

    mybatis 二级缓存不推荐使用 一 mybatis的缓存使用. 大体就是首先根据你的sqlid,参数的信息自己算出一个key值,然后你查询的时候,会先把这个key值去缓存中找看有没有value,如 ...

  5. MPI 集合通信函数 MPI_Scatterv(),MPI_Gatherv(),MPI_Allgatherv(),MPI_Alltoall(),MPI_Alltoallv(),MPI_Alltoallw()

    ▶ 函数 MPI_Scatterv() 和 MPI_Gatherv() .注意到函数 MPI_Scatter() 和 MPI_Gather() 只能向每个进程发送或接受相同个数的元素,如果希望各进程获 ...

  6. Windows下测量cpu利用率的类

    方法参考网上的一段代码,把它改成了类以方便调用,很简单,但是可以一用,哈哈. cpu_usage.h #include <windows.h> class CpuUsage { publi ...

  7. Selenium Webdriver——设置等待时间

    1.隐式等待 implicitlyWait(): 当使用了隐士等待执行测试的时候,如果 WebDriver没有在 DOM中找到元素,将继续等待,超出设定时间后则抛出找不到元素的异常 当查找元素或元素并 ...

  8. Kafka学习整理五(Consumer配置)

    Property Default Description group.id   用来唯一标识consumer进程所在组的字符串,如果设置同样的group id,表示这些processes都是属于同一个 ...

  9. shell如何传递变量到另一个脚本文件中

    http://www.jbxue.com/article/shell/20707.html本文介绍了shell脚本传递变量到另一个脚本文件中的方法,在脚本中调用另一脚本,即创建了一个子进程,感兴趣的朋 ...

  10. linux 关闭电子邮件传输服务

    1.检查sendmail的哪些runlevel开启: chkconfig --list sendmail