`
jackleechina
  • 浏览: 573424 次
  • 性别: 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()的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

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

    <SPAN xss=removed>1.Math... 2.Math.floor(num); 参数num为一个数值,函数结果为num的整数部分(返回小于等于n的最大整数)。  Math.floor(num); 参数num为一个数值,函数结果为num的整数部分(返回小于等于n的最大整数

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

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

    加密和解密

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

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

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

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

    JavaScript权威指南

    Java-to-JavaScript Data Conversion Part III: Core JavaScript Reference Chapter 23. Core JavaScript Reference Sample Entry arguments[ ] Arguments Arguments.callee Arguments.length ...

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

    js生成随机数的过程解析

    Math.ceil(); //向上取整。 Math.floor(); //向下取整。 Math.round(); //四舍五入。 Math.random(); //0.0 ~ 1.0 之间的一个伪随机数。【包含0不包含1】 //比如0.8647578968666494 Math.ceil(Math.random()*10); //...

    学习笔记(六)JavaScript 内建对象 Math

    Math.ceil(x) 对数进行上舍入 Math.floor(x) 对数进行下舍入 Math.round(x) 把数四舍五入为最接近的整数 Math.max(x,y) 返回 x 和 y 中的最高值 Math.min(x,y) 返回 x 和 y 中的最低值 Math.pow(x,y) 返回...

    初级Java程序员面试题

    JDK 和 JRE 有什么区别? JDK(Java Development Kit),Java开发工具包 JRE(Java Runtime Environment),Java运行环境 JDK中包含JRE,JDK中有一个名为jre的...Math提供了三个与取整有关的方法:ceil、floor、round

Global site tag (gtag.js) - Google Analytics