當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java StrictMath abs()用法及代碼示例

StrictMath類abs()方法

用法:

    public static float abs(float f);
    public static int abs(int i);
    public static long abs(long l);
    public static double abs(double d);
  • abs() 方法在 java.lang 包中可用。
  • 這些方法用於返回方法中給定參數的絕對值。
  • 這些方法不會拋出異常。
  • 這些是靜態方法,可以通過類名訪問,如果我們嘗試使用類對象訪問這些方法,則不會出現任何錯誤。

參數:

  • int / long / float / double– 表示要找到其絕對值的值。

返回值:

這個方法的返回類型是int / long / float / double– 它返回給定值的絕對值。

注意:

  • 如果我們傳遞一個正值,則返回相同的值。
  • 如果我們傳遞一個負值,則返回沒有符號的相同值。
  • 如果我們傳遞一個零,則返回相同的值(零)。
  • 如果我們傳遞一個 NaN,則返回 NaN。

例:

// Java program to demonstrate the example 
// of abs() method of StrictMath class

public class Abs {
    public static void main(String[] args) {

        // variable declarations
        double a = 123.121d;
        double b = -123.121d;

        int c = 123121;
        int d = -123121;

        long e = 123121l;
        long f = -123121l;

        float g = 123.121f;
        float h = -123.121f;

        // Display previous value of a,b  
        System.out.println("a:" + a);
        System.out.println("b:" + b);

        // Display previous value of c,d
        System.out.println("c:" + c);
        System.out.println("d:" + d);

        // Display previous value of e,f 
        System.out.println("e:" + e);
        System.out.println("f:" + f);

        // Display previous value of g,h
        System.out.println("g:" + g);
        System.out.println("h:" + h);


        System.out.println();
        System.out.println("abs(double):");

        // By using abs(double d) method we will calculate the 
        //absolute value of given parameter in the method

        System.out.println("StrictMath.abs(a):" + StrictMath.abs(a));
        System.out.println("StrictMath.abs(b):" + StrictMath.abs(b));

        System.out.println();
        System.out.println("abs(int):");

        // By using abs(int i) method we will calculate the 
        // absolute value of given parameter in the method

        System.out.println("StrictMath.abs(c):" + StrictMath.abs(c));
        System.out.println("StrictMath.abs(d):" + StrictMath.abs(d));

        System.out.println();
        System.out.println("abs(long):");

        // By using abs(long l) method we will calculate the 
        // absolute value of given parameter in the method

        System.out.println("StrictMath.abs(e):" + StrictMath.abs(e));
        System.out.println("StrictMath.abs(f):" + StrictMath.abs(f));

        System.out.println();
        System.out.println("abs(double):");

        // By using abs(double d) method we will calculate the 
        // absolute value of given parameter in the method

        System.out.println("StrictMath.abs(g):" + StrictMath.abs(g));
        System.out.println("StrictMath.abs(h):" + StrictMath.abs(h));
    }
}

輸出

a:123.121
b:-123.121
c:123121
d:-123121
e:123121
f:-123121
g:123.121
h:-123.121

abs(double):
StrictMath.abs(a):123.121
StrictMath.abs(b):123.121

abs(int):
StrictMath.abs(c):123121
StrictMath.abs(d):123121

abs(long):
StrictMath.abs(e):123121
StrictMath.abs(f):123121

abs(double):
StrictMath.abs(g):123.121
StrictMath.abs(h):123.121


相關用法


注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java StrictMath abs() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。