Java - java.lang.Math クラス


■ページ目次

Top

■java.lang.Math

Top
■フィールド
Top
■おもなメソッド
Top
■例題
  1. Mathクラスのつぎのメソッドを確認するアプリケーションアプリケーション"Math01.java"を作成し、実行してください。
    1. public class Math01 {
    2. public static void main(String[] args) {
    3. double d = Double.parseDouble(args[0]);
    4. System.out.print("Math.round( " + d + ") = " + Math.round(d));
    5. System.out.print(" ");
    6. System.out.println(" Math.round(" + (-d) + ") = " + Math.round(-d));
    7. System.out.print(" Math.ceil( " + d + ") = " + Math.ceil(d));
    8. System.out.print(" ");
    9. System.out.println(" Math.ceil(" + (-d) + ") = " + Math.ceil(-d));
    10. System.out.print("Math.floor( " + d + ") = " + Math.floor(d));
    11. System.out.print(" ");
    12. System.out.println("Math.floor(" + (-d) + ") = " + Math.floor(-d));
    13. System.out.print(" (int)( " + d + ") = " + (int)(d));
    14. System.out.print(" ");
    15. System.out.println(" (int)(" + (-d) + ") = " + (int)(-d));
    16. }
    17. }

    □ 実行結果

    $ java Math01 3.14
    Math.round( 3.14) = 3    Math.round(-3.14) = -3
     Math.ceil( 3.14) = 4.0   Math.ceil(-3.14) = -3.0
    Math.floor( 3.14) = 3.0  Math.floor(-3.14) = -4.0
         (int)( 3.14) = 3         (int)(-3.14) = -3
    
    $ java Math01 6.5
    Math.round( 6.5) = 7    Math.round(-6.5) = -6
     Math.ceil( 6.5) = 7.0   Math.ceil(-6.5) = -6.0
    Math.floor( 6.5) = 6.0  Math.floor(-6.5) = -7.0
         (int)( 6.5) = 6         (int)(-6.5) = -6
    

Top
inserted by FC2 system