Math ceil(),floor(),round()方法的使用

Math的几个方法Math.round()Math.ceil()Math.floor()Math.abs()记录一下 Math.round() 就是数学中的四舍五入,举例: System.out.println("Math.round(1.2)="+Math.round(1.2)); System.out.println("Math.round(1.5)="+Math.round(1.5)); System.out.println("Math.round(1.7)="+Math.round(1.7)... 阅读详情

floor 返回不大于的最大整数
round 则是4舍5入的计算,入的时候是到大于它的整数(当-1.5时可见,四舍五入后得到的结果不是我们期待的,解决办法是先对他取绝对值,然后在用round方法)

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


ceil 则是不小于他的最小整数

看例子

 Math.floorMath.roundMath.ceil
1.411 2
1.512 2
1.612 2
-1.4-2-1 -1
-1.5-2-1 -1
-1.6-2-2 -1


测试程序如下:

  1. public class MyTest {   
  2.   public static void main(String[] args) {   
  3.     double[] nums = { 1.41.51.6, -1.4, -1.5, -1.6 };   
  4.     for (double num : nums) {   
  5.       test(num);   
  6.     }   
  7.   }   
  8.   
  9.   private static void test(double num) {   
  10.     System.out.println("Math.floor(" + num + ")=" + Math.floor(num));   
  11.     System.out.println("Math.round(" + num + ")=" + Math.round(num));   
  12.     System.out.println("Math.ceil(" + num + ")=" + Math.ceil(num));   
  13.   }   
  14. }  



运行结果
Math.floor(1.4)=1.0
Math.round(1.4)=1
Math.ceil(1.4)=2.0
Math.floor(1.5)=1.0
Math.round(1.5)=2
Math.ceil(1.5)=2.0
Math.floor(1.6)=1.0
Math.round(1.6)=2
Math.ceil(1.6)=2.0
Math.floor(-1.4)=-2.0
Math.round(-1.4)=-1
Math.ceil(-1.4)=-1.0
Math.floor(-1.5)=-2.0
Math.round(-1.5)=-1
Math.ceil(-1.5)=-1.0
Math.floor(-1.6)=-2.0
Math.round(-1.6)=-2
Math.ceil(-1.6)=-1.0

转自:http://blog.csdn.net/foart/article/details/4295645
动态知识图谱构建:从本体论到工程实践 知识图谱作为语义网和人工智能领域的核心技术,旨在通过结构化的方式表示和组织知识。其核心原理在于将实体、概念及其关系建模为图结构,从而支持复杂的语义推理和关联查询。这一技术为机器理解数据间的深层联系提供了基础,在智能推荐、企业知识管理和学术研究等领域具有重要价值。通过结合自然语言处理(NLP)和图数据库技术,系统能够从非结构化数据中自动抽取知识,并构建动态演化的认知网络。本文以“本体论章鱼”项目为例,探讨了如何实现从静态本体到动态知识网络的范式转变,并详细解析了其分布式感知、适应性学习的架构设计,为构建灵活可 阅读详情

相关推荐

【第1章>第8节】FNN模糊神经网络的理论学习与MATLAB仿真

模糊神经网络(FNN,Fuzzy Neural Network)是将模糊逻辑与神经网络相结合的一种智能算法,它融合了模糊逻辑善于处理不确定性和神经网络的自学习、自适应能力。

FPGA/MATLAB学习教程/源码/项目合作开发 372

Math.floorMath.ceilMath.rint,Math.round用法详解

Math.floor(), Math.ceil() ,Math.round(), Math.rint(),

冬青的专栏 1539

基于计算机视觉的塑料齿轮齿形缺陷检测 贺秋伟 , 王龙山 , 陈向伟 , 高立国

于CCD图像的塑料齿轮齿形缺陷检测方法,基于计算机视觉的塑料齿轮齿形缺陷检测

math.ceil()的用法

math.ceil(x) # 返回不小于x的最接近的整数 math.ceil(0.33) Out[3]: 1 math.ceil(0.93) Out[4]: 1

qq_35037684的博客 1万+

math.ceil()math.floor()函数

math.floor() 函数将 返回数字向下舍入到最接近的整数。如果数字已经是整数,则返回相同的数字。math.ceil() 函数返回大于数字的最小整数值。如果数字已经是整数,则返回相同的数字。

woshicaiji12138的博客 833

前端Math方法总结

Math.trunc()函数,返回的是一个数的整数部分,不管正数还是负数,直接去掉小数点及之后的部分。Math.min()方法,是可以返回指定一组数据中最小值。

Lydia 933

js中Math.floorMath.ceilMath.round和parseInt小数取整小结

向下的意思是往小的方向取,如Math.round(-8.6)先-8.6+0.5=-8.1,再向下取整得到-9)Math.floor(-1.5)  //返回-2Math.floor(-5.8)  //返回-6Math.round(-1.5)  //返回-1Math.round(-5.8)  //返回-6Math.ceil(-1.5)  //返回-1Math.ceil(-5.8)  //返回-5。parseInt(-1.5)  //返回-1。parseInt(-5.8)  //返回-5

weixin_61843526的博客 3343

Math ceil()floor()round()方法

HI,我是小瑞! 7万+

Parseint()Math.round()Math.floor()Math.ceil()四种取整方法的区别

parseInt()方法取整是把小数点后面小数去掉,只保留整数部分。parseInt作用: 1.函数可解析一个字符串,并返回一个整数。console.log(parseInt(19.11));//返回19 2.舍去小数点后的数字不进行四舍五入。 console.log(parseInt(19.99));//19 3.可以转换进制。 console.log(parseI...

weixin_44600235的博客 1691

Math工具类的ceil()floor()round()方法源码阅读

以double数据类型为例:0表示这个浮点数为正数1表示这个浮点数为负数计算一个double数值类型的数据data = (-1)^S * M * 2^E在计算机中存储时,阶码采用移码-1的方式存储,尾数只存储小数部分移码就是对补码的符号位进行取反。 Math.ceil(double)里面调用了StrictMath.ceil(double);Math.flooor(double)里面调用了StrictMath.floor(double)。在 StrictMath类中的ceil(double)和floor(do

qq_56460466的博客 853

parseInt()Math.round()Math.floor()Math.ceil()四种取整方法的区别

在最近的js练习中经常会用到随机数的使用,而使用随机数时大部分会对齐进行取整。   parseInt()方法取整是把小数点后面小数去掉,只保留整数部分。如果要取整的数为正时,类似Math.floor();为负时,类似Math.ceil()   Math.round() 四舍五入取整   Math.floor() 向下取整  如Math.floor(1.8) 返回 1Math.floor

锦瑟无端五十弦 5959

java Math.round()Math.ceil()Math.floor()取整方法的区别

Math.round() “四舍五入”,就看小数点后第一位的值小数点后第一位5小数点后第一位=5 Math.ceil():(ceil:天花板)向上取整,及小数部分,直接不管,整数部分加1 Math.floor():(floor:地板)向下取整,小数点后的,直接丢掉 【注】 Math.floor()容易出现精度问题,举个最简单例子:对小数 8.54 保留两位小数(虽然它已经保留了 2 位小数):Math.floor(8.54*100)/100 // 输出结果为 8.53, 注意...

dayuiicghaid的博客 717

js Math对象的round(),ceil(),floor()方法

1Math.round() 功能:四舍五入取整。 语法:Math.round(x) 参数: x:一个浮点数。 返回值:与x最接近的整数。 注:当出现两个最接近的整数,将返回最大的那个整数。 示例: Math.round(1.3);//1 Math.round(1.5);//2 Math.round(1.7);//2 Math.round(-1.3);//-1 Math.round(-1.5);//-1 Math.round(-1.7);//-2 2Math.floo...

旭东怪的博客 1781

Android中MathMath.floor()Math.round()Math.ceil()方法使用

1Math.floor()先看定义:/** * Returns the double conversion of the most positive (closest to positive * infinity) integer value less than or equal to the argument. * <p> * Special cases: * <ul> * <li>

Just do it 1万+

Mathceilfloorround方法详解及示例

Mathceilfloorround方法详解及示例

韩金群的博客 1万+

Java Math.floor()Math.ceil()Math.round()四舍五入的使用及区别

Math.floor()Math.ceil()Math.round()都是对浮点数取整(floorceil返回不带小数的double,round返回long) floor字面意思为地板,返回的是小于或等于该数值的最大的整数 ceil字面意思是天花板,返回的是大于或等于该值的最小的整数 round就是我们常见的四舍五入,不再多说 直接看下demo及运行结果 public class ...

野猿新一 2533

Math类四个常用方法辨析,floorceilround、rint

Math.floorfloor,英文原意:地板。 Math.floor 函数是求一个浮点数的地板,就是 向下 求一个最接近它的整数,它的 值肯定会小于或等于这个浮点数。再看下面的例子的时候,脑中想象一个竖着的数轴,负数在下,正数在上。这样对于向上取整和向下取整的说法,可能会更容易理解。例子: System.out.println("Math.floor(-1.1): " + Mat

自己走的路 1万+

Math类中roundceilfloor方法的功能

Java中的Math工具类用来完成除+-、*、/、%等基本运算以外的复杂运算,位于java.lang包下,Math类的构造器全是私有的(private),因此无法创建Math类的对象,Math类的方法全是类方法,可以直接通过类名来调用它们。下面重点介绍Math类中经常用到的几个方法,也是面试时经常被问到的知识点。 1round round方法表示四舍五入。round意为“环绕”,其原理是在...

weixin_30666401的博客 262

Math类中round,ceilfloor方法的功能各是什么

round,ceilfloor方法位于Math类中,Math是一个包含了很多数学常量与计算方法的类,位于java.lang包下,能自动导入,而且Ma't'h类中的方法全是静态方法,下面重点介绍三个方法的含义。 (1round方法表示四舍五入,round英文的意思是“环绕”,其实现原理是在原来数字基础之上先增加0.5,然后在向下取整,等同于(int)Math.floor(x + 0.5f)他的...

sdgames的博客 736

Python教程:ceilfloorround、int取整

round只是针对小数点后.5的情况会按照规律计算,因为存储时不同,例如:4.5存储时为4.4999999…”整除取整“符号运算实现向下取整,与 math.floor() 方法效果一样。math.ceil() 严格遵循向上取整,所有小数都向着数值更大的方向取整。int() 向0取整,取整方向总是让结果比小数的绝对值更小。同 math.ceil 类似,方向相反,向下取整。round() 方法返回浮点数的四舍五入值。不传第二个参数时,默认取整,四舍五入。

Python热爱者的博客 1628
上一篇: 关于复杂数据存储的问题--基础篇:数组以及浅拷贝与深拷贝的问题
下一篇: 守护(daemon)线程
road_16
博客等级 码龄15年 7粉丝 5原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值