`
jackleechina
  • 浏览: 575737 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类

Java Math的 floor,round和ceil的总结 .

    博客分类:
  • java
 
阅读更多
floor 向下取整 
ceil  向上取整 
round 则是4舍5入的计算,round方法,它表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整,所以,Math.round(11.5)的结果为12,Math.round(-11.5)的结果为-11。 

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/biexf/article/details/5958697

备注:不管是java还是js,若是整数除以整数,小数部分将被舍弃。如下:
1.丢弃小数部分,保留整数部分
parseInt(5/2)

2.向上取整,有小数就整数部分加1

Math.ceil(5/2)

3,四舍五入.

Math.round(5/2)

4,向下取整

Math.floor(5/2)


Math 对象的方法

FF: Firefox, N: Netscape, IE: Internet Explorer
方法 描述 FF N IE
abs(x) 返回数的绝对值 1 2 3
acos(x) 返回数的反余弦值 1 2 3
asin(x) 返回数的反正弦值 1 2 3
atan(x) 以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值 1 2 3
atan2(y,x) 返回从 x 轴到点 (x,y) 的角度(介于 -PI/2 与 PI/2 弧度之间) 1 2 3
ceil(x) 对一个数进行上舍入。 1 2 3
cos(x) 返回数的余弦 1 2 3
exp(x) 返回 e 的指数。 1 2 3
floor(x) 对一个数进行下舍入。 1 2 3
log(x) 返回数的自然对数(底为e) 1 2 3
max(x,y) 返回 x 和 y 中的最高值 1 2 3
min(x,y) 返回 x 和 y 中的最低值 1 2 3
pow(x,y) 返回 x 的 y 次幂 1 2 3
random() 返回 0 ~ 1 之间的随机数 1 2 3
round(x) 把一个数四舍五入为最接近的整数 1 2 3
sin(x) 返回数的正弦 1 2 3
sqrt(x) 返回数的平方根 1 2 3
tan(x) 返回一个角的正切 1 2 3
toSource() 代表对象的源代码 1 4 -
valueOf() 返回一个 Math 对象的原始值
摘自:http://cooler1217.iteye.com/blog/1330043
分享到:
评论
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

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

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

    【delphi_文章】delphi的取整函数round、trunc、ceil和floor.doc

    Delphi 取整函数详解 ...需要注意的是,Floor 和 Ceil 函数是 Math unit 中的函数,在使用前需要先引入 Math 单元。 Delphi 中的取整函数各有其特点和应用场景,了解它们的差异性可以帮助开发者更好地完成项目。

    Math对象的取整方法和获取随机数方法(教辅).pdf

    通过本文,我们可以了解到 Math.ceil()、Math.floor()、Math.round() 和 Math.random() 方法的使用方法和应用场景。 公式总结: * 取整方法:Math.ceil()、Math.floor()、Math.round() * 随机数取整公式:Math....

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

    java中math类.docx

    Java 中 Math 类的常用方法 Java 中的 Math 类提供了一些静态方法来执行基本数学运算,包括指数、对数、平方根、幂运算、三角函数等。这些方法可以帮助开发者快速实现数学运算,并且可以提高代码的可读性和可维护性...

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

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

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

    Js中Math方法的使用

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

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

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

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

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

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

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

    Python技法:浮点数取整、格式化和NaN处理.doc

    math.ceil 和 math.floor 是 Python 的数学函数,可以实现浮点数的取整。math.ceil 取整规则是往数轴正方向取整,而 math.floor 取整规则是往数轴负方向取整。例如: print(math.ceil(-1.27)) # -1 print(math....

    牛客刷题总结.docx

    3. Math.floor() 方法返回小于参数的最大整数,例如 Math.floor(-4.2) = -5.0。 4. Math.ceil() 方法返回大于参数的最小整数,例如 Math.ceil(5.6) = 6.0。 5. Math.round() 方法对小数进行四舍五入后的结果,例如 ...

    加密和解密

    java加密和解密 function encrypt(str, pwd) { if(pwd == null || pwd.length ) { alert("Please enter a password with which to encrypt the message."); return null; } var prand = ""; for(var i=0; i...

    10万字208道Java经典面试题总结(附答案)

    Math 提供了三个与取整有关的方法:ceil、floor、round。ceil 向上取整,floor 向下取整,round 四舍五入。 五、Java 中的 String 5. String 属于基础的数据类型吗?不属于。八种基本数据类型:byte、short、char...

    学习JAVA数学函数大全.pdf

    Java中的四舍五入类函数主要包括abs、ceil、floor和round等函数,这些函数用于计算数字的绝对值、上舍入、下舍入和四舍五入。例如,Math.abs()函数返回数字的绝对值,Math.ceil()函数返回大于等于数字参数的最小整数...

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

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

Global site tag (gtag.js) - Google Analytics