A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 灰太狼爱平底锅1 中级黑马   /  2014-1-13 09:05  /  1567 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 灰太狼爱平底锅1 于 2014-1-26 22:00 编辑

为何Math.round(11.5)的结果为 12,Math.round(-11.5)的结果为-11,这是遵循了怎样的原则,求教?

评分

参与人数 1技术分 +1 收起 理由
FFF + 1 很给力!

查看全部评分

6 个回复

倒序浏览
四舍五入
11.5,小数位大于等于5,四舍五入就是12了
-11.5小数位大于等于5,四舍五入肯定会大于-11.5,就是-11了

评分

参与人数 1技术分 +1 收起 理由
FFF + 1 赞一个!

查看全部评分

回复 使用道具 举报
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,负数运算结果为整数部分。
总结:大于五全部加,等于五正数加,小于五全不加。

评分

参与人数 1技术分 +1 收起 理由
FFF + 1 很给力!

查看全部评分

回复 使用道具 举报
  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

评分

参与人数 1技术分 +1 收起 理由
FFF + 1 淡定

查看全部评分

回复 使用道具 举报
正数绝对值越大数越大
负数绝对值越大数越小
所以再加上四舍五入
正数.5进
负数.5遵循正数四舍五入原则,得忘意义大的方向变,即往绝对值小的方向变。
记住一点 小数点.5时,取大值,-11.5 当然是-11大了

评分

参与人数 1技术分 +1 收起 理由
FFF + 1 很给力!

查看全部评分

回复 使用道具 举报
四舍五入
回复 使用道具 举报
在 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;  
    }  

评分

参与人数 1技术分 +1 收起 理由
FFF + 1 赞一个!

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马