java的math常用方法


1: java取整

     a:floor向下取整

       用法:Math.floor(num)

       Math.floor(1.9)//1                      Math.floor(-1.9)//-2

    b:  round四舍五入

      用法:Math.round(num)實際上是等價於Math.floor(num+0.5)

      Math.round(1.5)//2                     Math.round(1.4)//1

      Math.round(-1.4)//-1                  Math.round(-1.5)//-1               Math.round(-1.6)//-2

    c:  ceil取不小於num的最小整數

       用法: Math.ceil(num)

       Math.ceil(1.4)//2      Math.ceil(1.5)//2             Math.ceil(1.6)//2

       Math.ceil(-1.4)//-1   Math.ceil(-1.5)//-1           Math.ceil(-1.6)//-1

    d:  神級方法直接加(int)強制轉換,直接去掉小數點位,沒有任何向上向下,需要時最好用的方法

 

2: java求絕對值

     Math.abs(num)

     Math.abs(-30.5)//30.5

3:   java隨機數

     Math.random()隨機去0~1的數

     (int)(100*Math.random())這樣就可以取0~100隨機整數

4: java冪函數

     Math.pow(a,b)a的b次方

     Math.pow(x,2)就是平方

     Math.pow(x,3)就是立方

5: java開根號

     Math.sqrt(num)num的平方根


免責聲明!

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



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