Math.round(11.5)等于多少? Math.round(-11.5)等于多少?
    1.先说下怎么理解

    round()方法可以这样理解:

    将括号内的数+0.5之后,向下取值,

    比如:round(3.4)就是3.4+0.5=3.9,向下取值是3,所以round(3.4)=3;

    round(-10.5)就是-10.5+0.5=-10,向下取值就是-10,所以round(-10.5)=-10

    所以,Math.round(11.5)=12;

    现在再来看,Math.round(11.5),Math.round(-11.5)你应该知道等于多少了吧,掌握了方法就好解决问题了。

    这个题面试了很多家就一家遇到,所以就来和大家分享下。

    扩展:常用的三个

    Math.ceil求最小的整数,但不小于本身.
    ceil的英文意义是天花板,该方法就表示向上取整,**

    例子:

    所以,Math.ceil(11.3)的结果为12,Math.ceil(-11.3)的结果是-11;

    1. /**
    2. • @see 求最小的整数,但不小于本身
    3. • @param double
    4. • @return double
    5. */
    6. System.out.println(Math.ceil(-1.1));
    7. System.out.println(Math.ceil(-1.9));
    8. System.out.println(Math.ceil(1.1));
    9. System.out.println(Math.ceil(1.9));
    10. 输出结果:
    11. -1.0
    12. -1.0
    13. 2.0
    14. 2.0

    Math.floor求最大的整数,但不大于本身.
    floor的英文意义是地板,该方法就表示向下取整,

    例子:

    floor的英文意义是地板,该方法就表示向下取整,

    所以,Math.floor(11.6)的结果为11,Math.floor(-11.6)的结果是-12;

    1. /**
    2. • @see 求最大的整数,但不大于本身
    3. • @param double
    4. • @return double
    5. */
    6. System.out.println(Math.floor(-1.1));
    7. System.out.println(Math.floor(-1.9));
    8. System.out.println(Math.floor(1.1));
    9. System.out.println(Math.floor(1.9));
    10. 输出结果:
    11. -2.0
    12. -2.0
    13. 1.0
    14. 1.0

    Math.round求本身的四舍五入.

    1. /**
    2. • @see 本身的四舍五入
    3. • @param double
    4. • @return long
    5. */
    6. System.out.println(Math.round(-1.1));
    7. System.out.println(Math.round(-1.9));
    8. System.out.println(Math.round(1.1));
    9. System.out.println(Math.round(1.9));
    10. 输出结果:
    11. -1
    12. -2
    13. 1
    14. 2

    Math.abs求本身的绝对值.

    1. /**
    2. • @see 本身的绝对值
    3. • @param double|float|int|long
    4. • @return double|float|int|long
    5. */
    6. System.out.println(Math.abs(1.1));
    7. System.out.println(Math.abs(1.9));
    8. System.out.println(Math.abs(-1.1));
    9. System.out.println(Math.abs(-1.9));
    10. 输出结果:
    11. 1.1
    12. 1.9
    13. 1.1
    14. 1.9

    Math.max与Math.min,比较两个数的最大值,最小值

    1. /**
    2. • @see 比较两个数的最大值,最小值
    3. • @param double|float|int|long
    4. • @return double|float|int|long
    5. */
    6. System.out.println(Math.max(1.0, 2.0));
    7. System.out.println(Math.min(-1.0, -2.0));
    8. 输出结果:
    9. 2.0
    10. -2.0

    返回一个与第二个参数相同的标志(正负号)的值

    1. /**
    2. • @see 返回一个与第二个参数相同的标志(正负号)的值
    3. • @param double|float
    4. • @return double|float
    5. */
    6. System.out.println(Math.copySign(-1.9, 2.9));
    7. System.out.println(Math.copySign(1.9, -2.9));
    8. System.out.println(Math.copySign(0.0, 2.9));
    9. System.out.println(Math.copySign(0.0, -2.9));
    10. 输出结果:
    11. 1.9
    12. -1.9
    13. 0.0
    14. -0.0