strictmath_Java StrictMath abs()方法与示例

strictmath

StrictMath类abs()方法 (StrictMath Class abs() method)

Syntax:

句法:

    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() method is available in java.lang package.

    abs()方法在java.lang包中可用。

  • These methods are used to return the absolute value of the given parameter in the method.

    这些方法用于返回方法中给定参数的绝对值。

  • These methods don't throw an exception.

    这些方法不会引发异常。

  • These are static methods, it is accessible with the class name and, if we try to access these methods with the class object then we will not get any error.

    这些是静态方法,可以通过类名进行访问,如果尝试使用类对象访问这些方法,则不会出现任何错误。

Parameter(s):

参数:

  • int / long / float / double – represents the value whose absolute value to be found.

    int / long / float / double-表示要找到其绝对值的值。

Return value:

返回值:

The return type of this method is int / long / float / double – It returns the absolute value of the given value.

此方法的返回类型为int / long / float / double-它返回给定值的绝对值。

Note:

注意:

  • If we pass a positive value, then the same value is returned.

    如果我们传递一个正值,则返回相同的值。

  • If we pass a negative value, then the same value without sign is returned.

    如果我们传递一个负值,则返回不带符号的相同值。

  • If we pass a zero, then the same value (zero) is returned.

    如果传递零,则返回相同的值(零)。

  • If we pass a NaN, then NaN is returned.

    如果我们通过NaN,则返回NaN。

Example:

例:

// 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));
    }
}

Output

输出量

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


翻译自: https://www.includehelp.com/java/strictmath-abs-method-with-example.aspx

strictmath

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值