floor 返回不大于的最大整数。

round 则是 4 舍 5 入的计算,入的时候是到大于它的整数。

round 表示"四舍五入",算法为Math.floor(x+0.5) ,即将原来的数字加上 0.5 后再向下取整,所以 Math.round(11.5) 的结果为 12,Math.round(-11.5) 的结果为 -11。

我们先通过以下实例来看看 Java Math floor、ceil、rint 及 round 各个方法的输出结果:

Test.java

importjava.util.Scanner;publicclassTest{publicstaticvoidmain(String[]args){Scannersc=newScanner(System.in);System.out.println("输入一个浮点数:");while(sc.hasNext()){doublenum=sc.nextDouble();System.out.println("Math.floor("+num+") ="+Math.floor(num));System.out.println("Math.ceil("+num+") ="+Math.ceil(num));System.out.println("Math.rint("+num+") ="+Math.rint(num));System.out.println("Math.round("+num+") ="+Math.round(num));}}}

执行以上程序测试输出:

$ javac Test.java

$ java Test

输入一个浮点数:

2.2

Math.floor(2.2) = 2.0

Math.ceil(2.2) = 3.0

Math.rint(2.2) = 2.0

Math.round(2.2) = 2

-2.2

Math.floor(-2.2) = -3.0

Math.ceil(-2.2) = -2.0

Math.rint(-2.2) = -2.0

Math.round(-2.2) = -2

-2.5

Math.floor(-2.5) = -3.0

Math.ceil(-2.5) = -2.0

Math.rint(-2.5) = -2.0

Math.round(-2.5) = -2

2.5

Math.floor(2.5) = 2.0

Math.ceil(2.5) = 3.0

Math.rint(2.5) = 2.0

Math.round(2.5) = 3

2.7

Math.floor(2.7) = 2.0

Math.ceil(2.7) = 3.0

Math.rint(2.7) = 3.0

Math.round(2.7) = 3

-2.7

Math.floor(-2.7) = -3.0

Math.ceil(-2.7) = -2.0

Math.rint(-2.7) = -3.0

Math.round(-2.7) = -3

解析输出

1、Math.floor() 向下取整,即小于这个数的最大的那个整数。

Math.floor(2.2) = 2.0;

Math.floor(-2.2) = -3.0;

Math.floor(2.5) = 2.0;

Math.floor(-2.5) = -3.0;

Math.floor(2.7) = 2.0;

Math.floor(-2.7) = -3.0;

2、Math.ceil() 向上取整,即大于这个数的最小的那个整数。

Math.ceil(2.2) = 3.0;

Math.ceil(-2.2) = -2.0;

Math.ceil(2.5) = 3.0;

Math.ceil(-2.5) = -2.0;

Math.ceil(2.7) = 3.0;

Math.ceil(-2.7) = -2.0;

3、Math.rint() 返回最接近该值的那个整数。

注意: 如果存在两个这样的整数,则返回其中的偶数。

4、Math.round() 四舍五入,但当参数为负数时不太好理解,直接上源码应该比较好理解,注意返回的是整型。

Math.round(x) = Math.floor(x + 0.5)

把以上代码带入以下实例测试:

Math.round(2.2) = 2;

Math.round(-2.2) = -2;

Math.round(2.5) = 3;

Math.round(-2.5) = -2;

Math.round(2.7) = 3;

Math.round(-2.7) = -3

参考地址:https://blog.csdn.net/u012050154/article/details/52448260

math.floor java_Java Math floor、ceil、rint 及 round 用法相关推荐

  1. math.floor java_Java Math.floor() 方法

    Java Math.floor() 方法 java.lang.Math.floor() 返回最大的(最接近正无穷大)double值,该值小于或等于参数,并等于某个整数. 1 语法 public sta ...

  2. math.hypot java_Java math

    Java math 函数 JavaMath类提供了几种用于数学计算的方法,例如min(),max(),avg(),sin(),cos(),tan(),round(),ceil(),floor(),ab ...

  3. math.hypot java_Java Math.hypot() 方法

    Java Math.hypot() 方法 java.lang.Math.hypot() 用于返回的指定的参数的平方和的平方根,没有中间溢出或下溢. 1 语法 public static double ...

  4. math.hypot java_Java Math hypot()用法及代码示例

    java.lang.Math.hypot()函数是Java中的内置数学函数,可返回欧几里得范数, .函数返回sqrt(x2+ y2),而不会出现中间上溢或下溢. 如果任何一个参数都是无限大,则结果为正 ...

  5. Math.ceil(),Math.floor()与Math.round()三个函数的定义。

    为什么80%的码农都做不了架构师?>>>    JavaScript: The Definitive Guide, 4th Edition中对Math.ceil(),Math.flo ...

  6. math.floor()和math.ceil()的使用

    math.floor()和math.ceil()的使用 math.floor是向下取整数: math.ceil是向上取整数:

  7. .NET中的Math.Floor()和Math.Truncate()什么区别?

    Math.Floor()符合IEEE标准754第4节"向负无穷大" Math.Floor() . Math.Truncate() " Math.Truncate()入到最 ...

  8. python学习笔记——floor函数与ceil函数、round函数、reduce函数

    floor函数用于向下取整,需要导入math模块才能使用. ceil函数用于向上取整,同样需要通过math模块才能使用. round函数用于保留指定位数,有类似功能的有三种函数格式化输出方法 用法ro ...

  9. php是舍五入,php四舍php四舍五入五入函数 floor函数、ceil函数、round

    PHP(外文名,中文名:超文本预处理器)是一种通用开源脚本语言.语法吸收了C语言.Java和Perl的特点,入门门槛较低,易于学习,使用广泛,主要适用于Web开发领域.PHP的文件后缀名为php. 本 ...

  10. paper 11:matlab中fix函数,floor函数,ceil函数,round函数的区分

    1)fix(n)朝零方向取整,这是一类应用在整数取值上的函数,就如同以前我们所研究的求整问题,例如,fix(pi)=3;fix(-1.3)=-1; fix(1.3)=1; 2)round(n):四舍五 ...

最新文章

  1. GO语言有哪些优势?怎样入门?
  2. 【架构】典型的 K8s 架构图-核心概念(简化)
  3. 洛谷 - P4556 [Vani有约会]雨天的尾巴 /【模板】线段树合并(树上差分+线段树合并)
  4. kafka管理神器-kafkamanager
  5. python打印所有花数_Python中使用while循环实现花式打印乘法表
  6. P2827-蚯蚓【队列】
  7. Python Flask 中的路由
  8. JPA、Hibernate、Mybatis的区别
  9. AT2112 Non-redundant Drive
  10. 浅谈Javascript -- 【嵌套函数及闭包】
  11. validate参数校验和数据回显
  12. 计算机组成原理尾数的求法,计算机组成原理第八讲(运算办法).ppt
  13. Atitit java zip compress use apache tool jar 压缩的问题 static void zip(java.lang.String zipFileName,
  14. Scrapy爬取并分析CSDN热门文章标题
  15. Linux快捷键详解
  16. 微信小程序人脸识别认证-微信开放接口
  17. 项目经理面试的一些问题讨论
  18. 该死的强迫症,教你stm32怎么把杜邦线弄整齐
  19. vue实现更换背景图片_Vue实现背景更换颜色操作
  20. Barefoot Networks在其下一代网络中实现无以伦比的可视化和高效化

热门文章

  1. Nature:Jeff Dangl团队揭示贪噬菌属在微生物组中维持根的生长
  2. Cadence仿真笔记(二):传统noise仿真—共源极的噪声
  3. 【CV算法兵器】“晓风残月” ->对抗攻击“兵器”大放送(综述篇)
  4. 苹果应用审核注意事项
  5. 各种 Python 库/模块/工具
  6. pandas入门与数据准备与简单筛选统计
  7. 【备忘】总结一些Java学习者经常去的网站及论坛
  8. Unity -- UI -- Scroll Rect
  9. 实训(一)学生管理系统
  10. 全国计算机网络英语,2007年10月自考试题计算机网络管理全国试卷(国外英语资料).doc...