• 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;

    Java代码  收藏代码
    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));  

     输出结果:

    Java代码  收藏代码
    1. -1.0  
    2. -1.0  
    3. 2.0  
    4. 2.0  

    Math.floor求最大的整数,但不大于本身.

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

    例子:

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

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

    Java代码  收藏代码
    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));  

      

    输出结果:

    Java代码  收藏代码
    1. -2.0  
    2. -2.0  
    3. 1.0  
    4. 1.0  

      

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

    Java代码  收藏代码
    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));  

     输出结果:

    Java代码  收藏代码
    1. -1  
    2. -2  
    3. 1  
    4. 2  

    Math.abs求本身的绝对值.   

    Java代码  收藏代码
    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));  

     输出结果:

    Java代码  收藏代码
    1. 1.1  
    2. 1.9  
    3. 1.1  
    4. 1.9  

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

    Java代码  收藏代码
    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));  

    输出结果:

    Java代码  收藏代码
    1. 2.0  
    2. -2.0  

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

    Java代码  收藏代码
    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));  

      

    输出结果:

    Java代码  收藏代码
    1. 1.9  
    2. -1.9  
    3. 0.0  
    4. -0.0  

    Math

    类的常用方法

     

    封装了一些基本运算方法,包括进行三角运算的正弦、余弦、正切、余切相关的方法:

    例如,求正弦的

    sin

    ,求余弦的

    cos

    等,如果使用的话可以参考

    JDK

     

    下面的方法可能是我们经常要使用的:

     

    1

    )求最大值,可以用于求

    int

    类型,

    long

    类型,

    float

    类型,

    double

    类型的最大值,

    下面仅仅下求整数最大值的方法的定义:

     

    public static int max(int a,int b); 

    2

    )求最小值,和求最大值基本相同。

     

    public static int min(int a,int b); 

    3

    )求绝对值,和求最大值的方法基本相同。

     

    public static int abs(int a) 

    4

    )四舍五入的方法

     

    public static int round(float a) 

    public static long round(double d) 

    5

    )计算幂

     

    public static double pow(double a,double b) 

    6

    )求下限值

     

    public static double floor(double d) 

    7

    )求上限值

     

    public static double ceil(double d) 

    8

    )求平方根

     

    public static double sqrt(double d) 

    下面的例子包含了上面的

    8

    个方法:

     

        double 

    d1 

    5.7; 

        double 

    d2 

    12.3; 

     

     

     

     

    double d3 = -5; 

     

     

     

     

     

        System.out.println(d1+"

    "+d2+"

    的最大值为:

    "+Math.max(d1,d2)); 

        System.out.println(d1+"

    "+d2+"

    的最小值为:

    "+Math.min(d1,d2)); 

        System.out.println(d3+"

    的绝对值为:

    "+Math.abs(d3)); 

        System.out.println(d2+"

    四舍五入之后为:

    "+Math.round(d2)); 

        System.out.println(d2+"

    2

    次幂为:

    "+Math.pow(d2,2)); 

        System.out.println(d2+"

    的下限为:

    "+Math.floor(d2)); 

        System.out.println(d2+"

    的上限为:

    "+Math.ceil(d2)); 

        System.out.println(d2+"

    的平方根为:

    "+Math.sqrt(d2)); 

    运行结果为:

     

    5.7

    12.3

    的最大值为:

    12.3 

    5.7

    12.3

    的最小值为:

    5.7 

    -5.0

    的绝对值为:

    5.0 

    12.3

    四舍五入之后为:

    12 

    12.3

    2

    次幂为:

    151.29000000000002 

    12.3

    的下限为:

    12.0 

    12.3

    的上限为:

    13.0 

    12.3

    的平方根为:

    3.5071355833500366 

    9

    )要获取一个随机数,如果是

    0

    1

    之间的随机数,可以直接使用下面的方法:

     

    public static double random(); 

    如果希望得到某个范围的随机数,例如

    60

    100

    ,可以这样处理:

     

        int 

    min=60; 

        int 

    max=100; 

        int 

    random; 

     

     

     

     

    random = min + (int) ( (max - min) * (Math.random()));

  • 相关阅读:
    javascript form表单常用的正则表达式
    jquery判断邮箱对错
    利用js实现placeholder占位符,甩开ie不兼容
    jquery常用的选择器
    html+css底部自动固定底部
    css form表单样式清除
    js alert(“”)弹框 自定义样式
    Vue.js 源码分析(二十三) 指令篇 v-show指令详解
    Vue.js 源码分析(二十一) 指令篇 v-pre指令详解
    Vue.js 源码分析(二十) 指令篇 v-once指令详解
  • 原文地址:https://www.cnblogs.com/cyl048/p/8516580.html
Copyright © 2020-2023  润新知