黑马程序员技术交流社区

标题: 关于Math的问题 [打印本页]

作者: 灰太狼爱平底锅1    时间: 2014-1-13 09:05
标题: 关于Math的问题
本帖最后由 灰太狼爱平底锅1 于 2014-1-26 22:00 编辑

为何Math.round(11.5)的结果为 12,Math.round(-11.5)的结果为-11,这是遵循了怎样的原则,求教?
作者: tuanjie    时间: 2014-1-13 09:10
四舍五入
11.5,小数位大于等于5,四舍五入就是12了
-11.5小数位大于等于5,四舍五入肯定会大于-11.5,就是-11了
作者: dapeng111    时间: 2014-1-13 09:34
1、小数点后第一位=5
2、正数:Math.round(11.5)=12
3、负数:Math.round(-11.5)=-11
4、
5、小数点后第一位<5
6、正数:Math.round(11.46)=11
7、负数:Math.round(-11.46)=-11
8、
9、小数点后第一位>5
10、正数:Math.round(11.68)=12
11、负数:Math.round(-11.68)=-12
根据上面运行结果,可以按照如下方式总结
1、参数的小数点后第一位<5,运算结果为参数整数部分。
2、参数的小数点后第一位>5,运算结果为参数整数部分绝对值+1,符号(即正负)不变。
3、参数的小数点后第一位=5,正数运算结果为整数部分+1,负数运算结果为整数部分。
总结:大于五全部加,等于五正数加,小于五全不加。
作者: 范晓冲    时间: 2014-1-13 09:39
  1. public class ProblemMath{
  2.                 public static void main(String[] args){
  3.                                 //Math类中的round()的返回值采取的原则是“四舍五入”,无论接收到的数是整数
  4.                                 //还是负数,都是统一的原则。
  5.                                 System.out.println("***************Math.round()***************");
  6.                                 System.out.println("Math.round(11.5)="+Math.round(11.5));
  7.                                 System.out.println("Math.round(-11.5)="+Math.round(-11.5));
  8.                                 System.out.println("Math.round(11.4)="+Math.round(11.4));
  9.                                 System.out.println("Math.round(-11.4)="+Math.round(-11.4));
  10.                                 //除了round()方法之外,Math类还有2个常用的方法,一个是floor(),
  11.                                 //此方法的返回值是小于接收到数的最大整数
  12.                                 System.out.println("***************Math.floor()***************");
  13.                                 System.out.println("Math.floor(11.5)="+Math.floor(11.5));
  14.                                 System.out.println("Math.floor(-11.5)="+Math.floor(-11.5));
  15.                                 System.out.println("Math.floor(11.4)="+Math.floor(11.4));
  16.                                 System.out.println("Math.floor(-11.4)="+Math.floor(-11.4));
  17.                                 //Math类中的另外一个经常被使用的方法是ceil(),
  18.                                 //此方法的返回值是大于接收到参数的最小整数。
  19.                                 System.out.println("***************Math.ceil()***************");
  20.                                 System.out.println("Math.ceil(11.5)="+Math.ceil(11.5));
  21.                                 System.out.println("Math.ceil(-11.5)="+Math.ceil(-11.5));
  22.                                 System.out.println("Math.ceil(11.4)="+Math.ceil(11.4));
  23.                                 System.out.println("Math.ceil(-11.4)="+Math.ceil(-11.4));
  24.                 }       
  25. }
复制代码



运行结果是:



M.png (81.61 KB, 下载次数: 11)

M.png

作者: 不冬眠的蚂蚁    时间: 2014-1-13 11:12
正数绝对值越大数越大
负数绝对值越大数越小
所以再加上四舍五入
正数.5进
负数.5遵循正数四舍五入原则,得忘意义大的方向变,即往绝对值小的方向变。
记住一点 小数点.5时,取大值,-11.5 当然是-11大了
作者: 黄晓鑫    时间: 2014-1-13 19:40
四舍五入
作者: mrwise1991    时间: 2014-1-13 22:58
在 java 中负数的四舍五入跟我们平常遇到的情况稍有不同,比如:-9.5,通常四舍五入后应该是  -10,而 java 中通过  java.lang.Math.round(-9.5)  运算后的结果是 -9,为什么呢?其实很简单,这个是 java 对 round 方法的定义导致的,看下 round() 方法的源代码就知道了,看起来这个方法有点偷懒:

[java] view plaincopy
/**
     * Returns the closest {@code int} to the argument, with ties
     * rounding up.
     *
     * <p>
     * Special cases:
     * <ul><li>If the argument is NaN, the result is 0.
     * <li>If the argument is negative infinity or any value less than or
     * equal to the value of {@code Integer.MIN_VALUE}, the result is
     * equal to the value of {@code Integer.MIN_VALUE}.
     * <li>If the argument is positive infinity or any value greater than or
     * equal to the value of {@code Integer.MAX_VALUE}, the result is
     * equal to the value of {@code Integer.MAX_VALUE}.</ul>
     *
     * @param   a   a floating-point value to be rounded to an integer.
     * @return  the value of the argument rounded to the nearest
     *          {@code int} value.
     * @see     java.lang.Integer#MAX_VALUE
     * @see     java.lang.Integer#MIN_VALUE
     */  
    public static int round(float a) {  
        if (a != 0x1.fffffep-2f) // greatest float value less than 0.5  
            return (int)floor(a + 0.5f);  
        else  
            return 0;  
    }  
  
    /**
     * Returns the closest {@code long} to the argument, with ties
     * rounding up.
     *
     * <p>Special cases:
     * <ul><li>If the argument is NaN, the result is 0.
     * <li>If the argument is negative infinity or any value less than or
     * equal to the value of {@code Long.MIN_VALUE}, the result is
     * equal to the value of {@code Long.MIN_VALUE}.
     * <li>If the argument is positive infinity or any value greater than or
     * equal to the value of {@code Long.MAX_VALUE}, the result is
     * equal to the value of {@code Long.MAX_VALUE}.</ul>
     *
     * @param   a   a floating-point value to be rounded to a
     *          {@code long}.
     * @return  the value of the argument rounded to the nearest
     *          {@code long} value.
     * @see     java.lang.Long#MAX_VALUE
     * @see     java.lang.Long#MIN_VALUE
     */  
    public static long round(double a) {  
        if (a != 0x1.fffffffffffffp-2) // greatest double value less than 0.5  
            return (long)floor(a + 0.5d);  
        else  
            return 0;  
    }  





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2