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

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

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
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)... 阅读详情

相关推荐

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

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

冬青的专栏 1539

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

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

woshicaiji12138的博客 833

math.ceil()的用法

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

qq_35037684的博客 1万+

前端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...

旭东怪的博客 1782

android 向下取整函数,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.* * Special cases:* * {@code floor(+0.0) = +...

weixin_35772595的博客 2527

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万+

Math类中round(), ceil(), floor()方法

1.round() 简单讲,四舍五入。但是要注意对于负数的情况 Math.round(-5.6) = - 6 Math.round(-5.4) = - 5 Math.round(5.6) = 6 Math.round(5.4) = 5 2.ceil() 3.floor()

Willow_zhu的博客 595

JavaScript Math对象 ceil()floor()round()方法

Math.ceil() 功能:对一个数进行上取整。 语法:Math.ceil(x) 参数: x:一个数值。 返回值:返回大于或等于x,并且与之最接近的整数。 注:如果x是正数,则把小数“入”;如果x是负数,则把小数“舍”。 例: document.write( Math.ceil(1.2)+", "+Math.ceil(1.8)+", "+Math.ceil(-1.2)+"

wangjuan_01的专栏 2万+

JS中Math对象pow(),abs(),ceil(),floor(),round()等常用方法

关于JS中Math对象常用的方法 JavaScript Math对象 Math对象多用来执行常见的算术方法Math.Pow(x,y) pow方法用于返回x的y次幂的值 x:底数 y:指数 console.log(Math.pow(2,4)); //输出16 Math.abs() abs方法用于返回一个绝对值 返回类型:number,如果不是数字返回NaN,0返回null。 console.l...

Peggy0829的博客 1251

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

ceil:向上取整,返回值float类型。 floor:向下取整,返回值float类型。 round:四舍五入,返回值int类型。 关于ceilfloor,想象一条y轴,不论负数正数,都向y轴的上或下方向取整。而四舍五入的原理是,把当前数加0.5,再向下取整。如Math.round(-1.5)的值是-1 ...

MOWRONG的博客 513
上一篇: 关于复杂数据存储的问题--基础篇:数组以及浅拷贝与深拷贝的问题
下一篇: 守护(daemon)线程
road_16
博客等级 码龄15年 7粉丝 5原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值