`
sd4886656
  • 浏览: 88502 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Math的floor,round和ceil的总结

 
阅读更多

 

 

floor 返回不大于的最大整数

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

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

 

 


  Math.floor Math.round Math.ceil
1.4 1 1 2
1.5 1 2 2
1.6 1 2 2
-1.4 -2 -1 -1
-1.5 -2 -1 -1
-1.6 -2 -2 -1

 

 

测试程序如下:

 

 

 

public class MyTest {
 public static void main(String[] args) {
  double[] nums = { 1.4, 1.5, 1.6, -1.4, -1.5, -1.6 };
  for (double num : nums) {
   test(num);
  }
 }
 private static void test(double num) {
  System.out.println("Math.floor(" + num + ")=" + Math.floor(num));
  System.out.println("Math.round(" + num + ")=" + Math.round(num));
  System.out.println("Math.ceil(" + num + ")=" + Math.ceil(num));
 }
}

 

 

    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
 

 

分享到:
评论
1 楼 tuspark 2015-08-09  
这里也有一篇文章,分析的更深刻一点:《Math.floor,Math.ceil,Math.rint,Math.round用法详解》

相关推荐

    js中Math之random,round,ceil,floor的用法总结.docx

    js中Math之random,round,ceil,floor的用法总结.docx

    js中Math之random,round,ceil,floor的用法总结

    本篇文章是对js中Math之random,round,ceil,floor的用法进行了总结介绍,需要的朋友可以过来参考下,希望对大家有所帮助

    JavaScipt中的Math.ceil() 、Math.floor() 、Math.round() 三个函数的理解

    Math.ceil(): round a number up Arguments: Any numeric value or expression Returns: The closest integer greater than or equal to x. ———————————————————————————————– Math...

    Javascript Math ceil()、floor()、round()三个函数的区别

    下面来介绍将小数值舍入为整数的几个方法:Math.ceil()、Math.floor()和Math.round()。 这三个方法分别遵循下列舍入规则: ◎Math.ceil()执行向上舍入,即它总是将数值向上舍入为最接近的整数; ◎Math.floor()执行...

    Java Math.round(),Math.ceil(),Math.floor()的区别详解

    主要介绍了Java Math.round(),Math.ceil(),Math.floor()的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    免费开源!! Java 面试必会 直通BAT

    Math 类中提供了三个与取整有关的方法:ceil、floor、round,这些方法的作用与它们的英 文名称的含义相对应,例如,ceil 的英文意义是天花板,该方法就表示向上取整, Math.ceil(11.3)的结果为12,Math.ceil(-11.3)的...

    Js中Math方法的使用

    Math方法使用: Math.ceil, Math.round, Math.floor

    js 使用 Math(算数) 对象来实现随机数的生成

    Math.ceil(Math.random()*10); // 获取从 1 到 10 的随机整数,取 0 的概率极小。Math.round(Math.random()); // 可均衡获取 0 到 1 的随机整数。Math.floor(Math.random()*10); // 可均衡获取 0 到 9 的随机整数。...

    Java程序设计基础:Math类.pptx

    Math类取整方法和random随机值方法 Math类 Math类 1 掌握常用数学函数; 2 会使用Math类中的方法解决数学问题。 double ceil( double x) //返回一个大于等于 x的最小整数值 Math.ceil(2.1) 结果为 3.0 Math.ceil(2.0...

    浅析JavaScript的几种Math函数,random(),ceil(),round(),floor()

    2、Math.ceil():返回值:返回大于或等于x,并且与之最接近的整数(如果x是正数,则把小数“入”;如果x是负数,则把小数“舍”)。 3、Math.round():四舍五入取整。 4、Math.floor():返回值:返回小于或等于x,并且...

    JavaScript Math 对象常用方法总结

    Math.ceil(x):向上取整 Math.floor(x):向下取整 Math.max(x,y):最大值 Math.min(x,y):最小值 Math.random(x):随机数 Math.round(x):四舍五入 获取指定范围内的随机数 var x=Math.floor(Math.random()*(max-min...

    详解Python3中ceil()函数用法

    描述 ceil(x) 函数返回一个大于或等于 x 的的最小整数。 语法 以下是 ceil() 方法的语法: import math math.ceil( x ) 注意:ceil()是不能直接访问的,需要...print (math.ceil(-45.17) : , math.ceil(-45.17)) pri

    JS小数转换为整数的方法分析

    Math.floor(12.9999) = 12 ceil:上进 Math.ceil(12.1) = 13; round: 四舍五入 Math.round(12.5) = 13 Math.round(12.4) = 12 二、小数位数控制 保留到整数: exam = Math.round(exam); 保留一位小数: exam = ...

    加密和解密

    var incr = Math.ceil(pwd.length / 2); var modu = Math.pow(2, 31) - 1; if(mult ) { alert("Algorithm cannot find a suitable hash. Please choose a different password. \nPossible considerations are to...

    js中常用的Math方法总结

    Math.ceil()执行向上舍入,即它总是将数值向上舍入为最接近的整数; Math.floor()执行向下舍入,即它总是将数值向下舍入为最接近的整数; Math.round()执行标准舍入,即它总是将数值四舍五入为最接近的整数; 例如:

    Javascript中字符串和数字的操作方法整理

    1、length – 返回字符串的长度 ‘abcd'.length; //4 2、Math.ceil(num) – 向上取整,不管小数点后面是多少,哪怕.00001,也会向上进一位。 Math.ceil(25.9); //26 Math.ceil(25.5);...4、Math.round

Global site tag (gtag.js) - Google Analytics