java.lang.Math's floor(),ceil() and round()的区别:
Just 贴 JDK:
Sources:
http://blog.csdn.net/foart/archive/2009/06/24/4295645.aspx
使用BigDecimal做精确的数值计算:
Java精确计算 - http://blog.csdn.net/xiaosu_521/article/details/2065854
关于round方法结合面试题再多说说:
Math.round(11.5) = 12
Math.round(-11.5) = -11
Math.round(-11.3) = -11
Math.round(-11.8) = -12
析:
Just 贴 JDK:
引用
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.
floor返回的是不大于(小于or等于)参数的最大整数的floating-point value表示。
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.
ceil返回的是不小于(大于or等于)参数的最小整数的floating-point value表示。
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)
在做四舍五入运算时,我们可以使用round;但当参数为 -xxx.5 时,四舍五入后得到的结果不是我们期待的,解决办法是先对参数取绝对值,然后再用round方法
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.
floor返回的是不大于(小于or等于)参数的最大整数的floating-point value表示。
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.
ceil返回的是不小于(大于or等于)参数的最小整数的floating-point value表示。
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)
在做四舍五入运算时,我们可以使用round;但当参数为 -xxx.5 时,四舍五入后得到的结果不是我们期待的,解决办法是先对参数取绝对值,然后再用round方法
Sources:
http://blog.csdn.net/foart/archive/2009/06/24/4295645.aspx
使用BigDecimal做精确的数值计算:
Java精确计算 - http://blog.csdn.net/xiaosu_521/article/details/2065854
关于round方法结合面试题再多说说:
Math.round(11.5) = 12
Math.round(-11.5) = -11
Math.round(-11.3) = -11
Math.round(-11.8) = -12
析:
引用