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()));


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM