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. 【并发编程】Future和FutureTask以及CompletionService

    Future接口 此接口主要用于: 代表异步计算的执行结果: 用于可取消的task:(比使用interrupt实现取消要方便 ) FutureTask类 FutureTask是Future的一个实现类 ...

  2. CentOS下长时间ping网络加时间戳并记录到文本

    Linux下长时间ping网络加时间戳并记录到文本   由于一些原因,比如需要检查网络之间是否存在掉包等问题,会长时间去ping一个地址,由于会输出大量的信息而且最好要有时间戳,因此我们可以使用简单的 ...

  3. Unity容器声明周期管理

    Having said that, here is a solution that you can use with the Unity container: Create some custom a ...

  4. MySQL 通用查询日志(General Query Log)

      同大多数关系型数据库一样,日志文件是MySQL数据库的重要组成部分.MySQL有几种不同的日志文件,通常包括错误日志文件,二进制日志,通用日志,慢查询日志,等等.这些日志可以帮助我们定位mysql ...

  5. 接口自动化(三)--读取json文件中的数据

    上篇讲到实际的请求数据放置在json文件内,这一部分记述一下python读取json文件的实现. 代码如下(代码做了简化,根据需要调优:可做一些容错处理): 1 import json 2 3 cla ...

  6. linux上安装oracle

    Linux上安装Oracle 10g:  http://69520.blog.51cto.com/59520/91156

  7. C#使用UUID生成ID

    tring id = System.Guid.NewGuid().ToString(); 一句话即可,但此时id中有“-”符号存在,使用下面语句可变为纯字母+数字. string id = Syste ...

  8. linux学习(别人指出来的), 回头有针对性的学下!

    应该是 会linux 基本操作吧linux 安装 lamp lnmp php拓展这些基本都得会把知道subversion 和 github 这俩吧windows的代码同步到linux上无需ftp 会跟 ...

  9. window.location 属性

    属性 含义 值 protocol: 协议 "http:" hostname: 服务器的名字 "b.a.com" port: 端口 "88" ...

  10. LevelDB 整体架构

    [LevelDB 整体架构]     从图中可以看出,构成LevelDb静态结构的包括六个主要部分:内存中的MemTable和Immutable MemTable以及磁盘上的几种主要文件:Curren ...