`
xiangxm
  • 浏览: 64738 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论
  • xiangxm: xurichusheng 写道一些涉及到密码保存的就需要加密/ ...
    3DES加密
  • xurichusheng: 一些涉及到密码保存的就需要加密/解密了。不需要解密的密码,如登 ...
    3DES加密

关于java.lang.Math类的round() ,floor() ,ceil() 方法

    博客分类:
  • Java
阅读更多
突然想起上次做题的时候我搞错了......我一直在误区中......于是写在这里警示一下。
其实许多大牛对于这个已经说了无数遍了,我就不多说了。

round()方法:

static long round(double a)
          Returns the closest long to the argument.
static int round(float a)
          Returns the closest int to the argument.

查看jdk文档,看看这几个方法到底是怎么回事。

public static long round(double a)
Returns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type long. In other words, the result is equal to the value of the expression: 

(long)Math.floor(a + 0.5d)
Special cases: 

If the argument is NaN, the result is 0. 
If the argument is negative infinity or any value less than or equal to the value of Long.MIN_VALUE, the result is equal to the value of Long.MIN_VALUE. 
If the argument is positive infinity or any value greater than or equal to the value of Long.MAX_VALUE, the result is equal to the value of Long.MAX_VALUE.

public static int round(float a)
Returns the closest int to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type int. In other words, the result is equal to the value of the expression: 

(int)Math.floor(a + 0.5f)

Special cases: 

If the argument is NaN, the result is 0. 
If the argument is negative infinity or any value less than or equal to the value of Integer.MIN_VALUE, the result is equal to the value of Integer.MIN_VALUE. 
If the argument is positive infinity or any value greater than or equal to the value of Integer.MAX_VALUE, the result is equal to the value of Integer.MAX_VALUE.


ceil()和floor()方法:
public static double ceil(double a)
Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer. Special cases: 
If the argument value is already equal to a mathematical integer, then the result is the same as the argument. 
If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument. 
If the argument value is less than zero but greater than -1.0, then the result is negative zero.
Note that the value of Math.ceil(x) is exactly the value of -Math.floor(-x). 

public static double floor(double a)
Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer. Special cases: 
If the argument value is already equal to a mathematical integer, then the result is the same as the argument. 
If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument.



基础很重要,有些东西不是相当然而,万丈高楼还的平地起!!
分享到:
评论

相关推荐

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

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

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

    首先还是看看[removed] The Definitive Guide, 4th Edition中对三个函数的定义。 Math.ceil(): round a number up ...Math.floor(): round a number down Arguments: Any numeric value or expressi

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

    oracle中函数 trunc(),round(),ceil(),floor的使用详解

    1.round函数(四舍五入) 描述 : 传回一个数值,该数值是按照指定的小数位元数进行四舍五入运算的结果 参数: number : 欲处理之数值 decimal_places : 四舍五入 , 小数取几位 ( 预设为 0 ) select round(123.456, ...

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

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

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

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

    Js中Math方法的使用

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

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

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

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

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

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

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

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

    Math.ceil(Math.random()*10);...Math.floor(Math.random()*10); // 可均衡获取 0 到 9 的随机整数。Math.round(Math.random()*10); // 基本均衡获取 0 到 10 的随机整数,其中获取最小值 0 和最大值 10 的几率少一半。

    axure的函数

    axure函数速查表,包括TotalDragY DragTime Window.width Window.height Window.scrollX Window.scrollY PageName toExponential(decimalPoints) ...Math.floor(x) Math.log(x) Math.max(x,y) Math.min(x,y)

    JavaScript Math.ceil 方法(对数值向上取整)

    JavaScript Math.ceil 方法 ...提示:该方法与 Math.floor 方法正好相反。 Math.ceil 方法实例 [removed] [removed]( Math.ceil(0.35) + ); [removed]( Math.ceil(10) + ); [removed]( Math

    JavaScript Math.ceil() 函数使用介绍

    Math.ceil(x) — 返回大于等于... ceil函数参数 x — 为number类型的数字 ceil函数返回值 返回大于等于x的最小整数 ceil函数示例 [removed](Math.ceil(5.99)); [removed](Math.ceil(-5.99)); [removed](Math.ceil(1.01)

    小游戏开发

    ...........................\.......\src\wyf\wpf\Ceil.java ...........................\.......\...\...\...\Constant.java ...........................\.......\...\...\...\Floor.java ....................

    JavaScript Math.floor方法(对数值向下取整)

    JavaScript Math.floor 方法 Math.floor 方法用于对数值向下取整,即得到小于或等于该数值的最大整数。语法如下: Math.floor(x) 参数说明: 参数 说明 x 必需。必须是一个数值。 提示:该方法与 Math.ceil ...

    java常用工具类的使用

    而Date的其他构造方法和普通方法的API都不容易实现国际化,因此目前Date类的大多数方法都被标识为过时,表示更灵活的时间类请参考java.util.Calendar。 Date的输出结果是按照国际通用格式输出的,而中国更习惯于...

    python源码math.py

    python源码,math.py,math.nan acos acosh asin asinh atan atan2 ceil

Global site tag (gtag.js) - Google Analytics